我们最近开始使用ImageResizer.Net http://www.imageresizing.net通过 GDI+ 在我们的 ASP.NET MVC 4 应用程序上动态调整图像大小。
有没有一种方法,仅使用 ImageResizer 来确定图像(作为字节数组读入)的实际分辨率(DPI、PPI,无论你想怎么称呼它)。我们目前有这样的工作流程,可以在需要时将图像大小调整为指定的较低分辨率:
//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 http://imageresizing.net/docs/plugins/basics.
新的 CustomSizing().Install(Config.Current);
或在resizer的配置部分 http://imageresizing.net/docs/configuration:
<plugins>
<add name="MyNamespace.CustomSizing" />
</plugins>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)