获得使用ImageResizer图像尺寸的最佳方法 [英] Best way to get image dimensions using ImageResizer

查看:1296
本文介绍了获得使用ImageResizer图像尺寸的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从切换家常用户文件上传现有的MVC 4的网站,因为他们上传的ImageResizer调整的文件。

我知道我不应该使用System.Drawing中的文档中,但我想不出抓住图像尺寸的任何其他方式。

如果尺寸是从原始图象或调整后的图像中,由于我preserving纵横比和仅仅需要确定的图像是横向或纵向不要紧。

我在这里加入code,我指的是我的评论回复@拿的回答。

  ImageJob IJ =新ImageJob(文件,requestedImageInfo:NULL);             诠释? Y = ij.SourceWidth;
             诠释? Z = ij.SourceHeight;


解决方案

如果你可以存储在图像尺寸上载过程中(从<一个href=\"https://github.com/imazen/resizer/blob/c4c586b58b2211ad0f48f7d8285e951ff6f262f9/Core/ImageJob.cs#L117\"相对=nofollow> ImageJob.SourceWidth /身高或LoadImageInfo),这是最好的,因为从文件中读取图像尺寸涉及到大量的I / O。

如果不是,ImageResizer提供<一个href=\"https://github.com/imazen/resizer/blob/e3cbd37a74e689ff68191c6129ef04de55686e19/Core/ImageBuilder.cs#L114\"相对=nofollow> IDictionary的LoadImageInfo(对象源,IEnumerable的请求的信息)方法在事后这样做。只要记住,但它涉及从磁盘读取,并且不希望把这个很多时候在一个HTTP请求。把这些数字在数据库中。

您可以随时通过 ImageBuilder.GetFinalSize计算图像的最终大小(originalSize,指令)。这一点,在另一方面,是非常快的,因为它不涉及I / O,只是数学。

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;

解决方案

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.

这篇关于获得使用ImageResizer图像尺寸的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆