Best way to get image dimensions using ImageResizer

拈花ヽ惹草 提交于 2019-12-09 03:50:45

问题


I am switching an existing MVC 4 website from home-cooked user file uploads to resizing files with ImageResizer as they are uploaded.

I see in the documentation that I should not use System.Drawing, but I can't figure out any other way of grabbing the image dimensions.

It does not matter if the dimensions are from the original image or a resized image, since I am preserving aspect ratio and merely need to determine if an image is landscape or portrait.

I am adding the code here that I refer to in my comment responding to @Nathanael's answer.

  ImageJob ij = new ImageJob(file, requestedImageInfo: null);

             int ? y = ij.SourceWidth;
             int ?  z = ij.SourceHeight;

回答1:


If you can store the image dimensions during upload (from ImageJob.SourceWidth/Height or LoadImageInfo), that is best, as reading image dimensions from a file involves lots of I/O.

If not, ImageResizer offers the IDictionary LoadImageInfo(object source, IEnumerable requestedInfo) method to do so after the fact. Just keep in mind, it does involve reading from disk, and you don't want to call this lots of times in a single HTTP request. Put those numbers in the database.

You can always calculate the final size of an image via ImageBuilder.GetFinalSize(originalSize, instructions). This, on the other hand, is very fast, as it involves no I/O, just math.



来源:https://stackoverflow.com/questions/27878738/best-way-to-get-image-dimensions-using-imageresizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!