减小大图像尺寸的最安全方法

发布于 2024-12-10 02:18:39 字数 1253 浏览 0 评论 0原文

我在这里和其他网站上花了很多时间试图找到一种重新采样图像的好方法。下面的代码是我正在使用的代码,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-12-17 02:18:39

请考虑使用第三方。

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).

也只是曾经 2024-12-17 02:18:39

因为它只发生在大图像上......
假设您的进程在图像上传后立即启动...

在您的进程开始处理图像之前,图像可能尚未完成复制。

要进行验证,请手动复制一张大图像。然后,开始你的流程。如果它有效,您就会知道您的过程没有问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文