减小大图像尺寸的最安全方法
我在这里和其他网站上花了很多时间试图找到一种重新采样图像的好方法。下面的代码是我正在使用的代码,
public static Bitmap ResampleImage(Image img, Size size, long quality) {
try {
var bmp = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppPArgb);
using (var gr = Graphics.FromImage(bmp)) {
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.DrawImage(img, new Rectangle(Point.Empty, size));
}
return setImageQualityLevel(bmp, quality);
}
catch (Exception ex) {
logger.Warn(string.Format("Image Resample Failed:\r\n{0}\r\n{1}", ex.Message, ex.StackTrace));
return (Bitmap)img;
}
}
不幸的是它经常无法触发 catch 块。大多数情况下,这是大图像(高达 20Mb) - 这些大小对我的应用程序没有任何好处,但我不想对我的用户说 - 在尝试使用它之前将其减少到 1Mb。我在使用类似这样的方法加载图像时也遇到 OOM 失败:
image2Use = new Bitmap(image.Filename);
因此,甚至在我将其放入重新采样代码之前它就失败了。
所以我真正寻找的是一些安全的方法来获取图像并重新调整其大小。我宁愿不去使用第三方库的开销,因为这就是我想要做的所有事情 - 加载一个大图像文件并使其小很多!
I spent quite a lot of time here and on other sites trying to find a good way to resample images. The code below is the one that I am using
public static Bitmap ResampleImage(Image img, Size size, long quality) {
try {
var bmp = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppPArgb);
using (var gr = Graphics.FromImage(bmp)) {
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.DrawImage(img, new Rectangle(Point.Empty, size));
}
return setImageQualityLevel(bmp, quality);
}
catch (Exception ex) {
logger.Warn(string.Format("Image Resample Failed:\r\n{0}\r\n{1}", ex.Message, ex.StackTrace));
return (Bitmap)img;
}
}
Unfortunately it regularly fails triggering the catch block. Mostly this is with big images (up to 20Mb) - These sizes are of no benefit in my application but I would rather not say to my users - cut it down to 1Mb before trying to use it. I also get OOM failures on loading the image using something like this:
image2Use = new Bitmap(image.Filename);
It is therefore failing even before I can get it into my resample code.
So what I am really looking for is some safe way to get an image in and re-size it. I would rather not go to the overhead of using a third party library since this is all I want to do - load a big image file and make it a lot smaller!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请考虑使用第三方。
ImageMagick 有一个 C# 界面
他们有一个很好的
convert
命令 - line 实用程序将完全满足您的需求。我已经使用它们好几年了,它们的库在不同环境(Windows 桌面一直到 *nix 服务器)中的易用性和功能给我留下了深刻的印象。
Please considered using a third-party.
ImageMagick has a C# interface
They have a nice
convert
command-line utility that will do exactly what you want.I've used them for several years now and have been very impressed with the ease of use and capabilities of their libraries in different environments (Windows Desktop all the way to *nix servers).
因为它只发生在大图像上......
假设您的进程在图像上传后立即启动...
在您的进程开始处理图像之前,图像可能尚未完成复制。
要进行验证,请手动复制一张大图像。然后,开始你的流程。如果它有效,您就会知道您的过程没有问题。
Since it only happens on large images...
And assuming your process starts immediately after the image uploads...
It's possible the image hasn't finished copying before your process starts to work on it.
To verify, hand copy one of the large images. Then, start your process on it. If it works, you'll know your process is OK.