使用ImageResizer.net确定图像的当前大小

我们最近开始在GDI +上使用ImageResizer.Net来动态调整ASP.NET MVC 4应用程序上的图像。

有没有办法,只使用ImageResizer,来确定图像的实际分辨率(DPI,PPI,无论你想要什么调用它)(以字节数组的forms读入)。 我们目前有这样的工作流程,在需要时将图像调整为指定的较低分辨率:

//pseudo-code var image = (Bitmap)Bitmap.FromStream(contentStream) var resX = image.HorizontalResolution; var resY = image.VerticalResolution; //calculate scale factor //determine newHeight and newWidth from scale var settings = new ResizeSettings("width={newWidth}&height={newHeight}") var newImage = ImageBuilder.Current.Build(image, someNewImage, settings); 

这很好,但它混合了GDI +和ImageResizer,并且有很多流打开和关闭相同的数据(实际代码有点冗长,有很多using语句)。

有没有办法只使用ImageResizer确定水平和垂直分辨率? 我无法立即在文档中找到任何内容。

目前,我们已经使用了托管API,但最终将使用MVC路由。

这是一个相当不典型的场景 – 通常传入的DPI值毫无价值。

但是,由于您似乎控制了这些值,并且需要它们来执行大小计算,我建议使用插件。 它们很简单,并且提供理想的性能,因为您不需要重复工作。

 public class CustomSizing:BuilderExtension, IPlugin { public CustomSizing() { } public IPlugin Install(Configuration.Config c) { c.Plugins.add_plugin(this); return this; } public bool Uninstall(Configuration.Config c) { c.Plugins.remove_plugin(this); return true; } //Executes right after the bitmap has been loaded and rotated/paged protected override RequestedAction PostPrepareSourceBitmap(ImageState s) { //I suggest only activating this logic if you get a particular querystring command. if (!"true".Equals(s.settings["customsizing"], StringComparison.OrdinalIgnoreCase)) return RequestedAction.None; //s.sourceBitmap.HorizontalResolution //s.sourceBitmap.VerticalResolution //Set output pixel dimensions and fit mode //s.settings.Width = X; //s.settings.Height = Y; //s.settings.Mode = FitMode.Max; //Set output res. //s.settings["dpi"] = "96"; return RequestedAction.None; } } 

可以通过代码或Web.Config进行安装。

new CustomSizing()。Install(Config.Current);

或者在缩放器的配置部分 :