帮助调整图像大小方法 gdi

发布于 2024-11-07 14:08:04 字数 2038 浏览 3 评论 0原文

所以我需要一些调整大小。

我发现了两种不同的方法。

一种看起来像这样:

public static Byte[] ResizeImageNew(System.Drawing.Image imageFile, int targetWidth, int targetHeight) {
        using(imageFile){
            Size newSize = CalculateDimensions(imageFile.Size, targetWidth, targetHeight);

            using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppRgb)) {
                newImage.SetResolution(imageFile.HorizontalResolution, imageFile.VerticalResolution);
                using (Graphics canvas = Graphics.FromImage(newImage)) {
                    canvas.SmoothingMode = SmoothingMode.AntiAlias;
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    canvas.DrawImage(imageFile, new Rectangle(new Point(0, 0), newSize));
                    MemoryStream m = new MemoryStream();
                    newImage.Save(m, ImageFormat.Jpeg);
                    return m.GetBuffer();
                }
            }
        }
    }

另一种:

    public static System.Drawing.Image ResizeImage(System.Drawing.Image originalImage, int width, int maxHeight) {
        originalImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
        originalImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

        int NewHeight = originalImage.Height * width / originalImage.Width;
        if (NewHeight > maxHeight) {
            // Resize with height instead
            width = originalImage.Width * maxHeight / originalImage.Height;
            NewHeight = maxHeight;
        }

        System.Drawing.Image newImage = originalImage.GetThumbnailImage(width, NewHeight, null, IntPtr.Zero);

        return newImage;
    }

我基本上“借用”了这两种方法,只是做了一些修改。

然而 - 使用第一个,每当我调整为小一点的图片时,文件的大小实际上比原始文件大(!?),

而第二个虽然大大提高了大小,但看起来很糟糕:/

我当然仅仅依靠如果可能的话,用第一种方法提高图像质量,但我看不出如何,从我的角度来看,一切看起来都是“高质量”?

So I'm in need of some resizing.

I found two different methods.

One looks like this:

public static Byte[] ResizeImageNew(System.Drawing.Image imageFile, int targetWidth, int targetHeight) {
        using(imageFile){
            Size newSize = CalculateDimensions(imageFile.Size, targetWidth, targetHeight);

            using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppRgb)) {
                newImage.SetResolution(imageFile.HorizontalResolution, imageFile.VerticalResolution);
                using (Graphics canvas = Graphics.FromImage(newImage)) {
                    canvas.SmoothingMode = SmoothingMode.AntiAlias;
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    canvas.DrawImage(imageFile, new Rectangle(new Point(0, 0), newSize));
                    MemoryStream m = new MemoryStream();
                    newImage.Save(m, ImageFormat.Jpeg);
                    return m.GetBuffer();
                }
            }
        }
    }

And the other:

    public static System.Drawing.Image ResizeImage(System.Drawing.Image originalImage, int width, int maxHeight) {
        originalImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
        originalImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

        int NewHeight = originalImage.Height * width / originalImage.Width;
        if (NewHeight > maxHeight) {
            // Resize with height instead
            width = originalImage.Width * maxHeight / originalImage.Height;
            NewHeight = maxHeight;
        }

        System.Drawing.Image newImage = originalImage.GetThumbnailImage(width, NewHeight, null, IntPtr.Zero);

        return newImage;
    }

I basically 'borrowed' both methods, and just changed bits and pieces.

However - using the first one, whenever I resize to a little smaller picture, the size of the file is actually larger then the original (!?)

And the second while greatly improving size looks terrible :/

I'm of course leaning on merely improving image quality in the first method, if possible, but I can't see how, everything looks to be 'high quality' from my point of view ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

染年凉城似染瑾 2024-11-14 14:08:04

您可能需要设置 JPEG 压缩级别。目前它可能以非常高的质量水平保存,这可能不是您想要的。

请参阅此处了解更多信息:http://msdn.microsoft.com/en-us /library/bb882583.aspx

但请注意,仅降低图像的分辨率并不一定会减少文件大小。由于压缩的工作原理,由于插值模式而变得模糊的较小分辨率文件可能会比原始文件大得多,但对于 JPEG,由于有损算法,这可能不是一个大问题。不过,如果原始文件之前非常简单(例如“平面”网络漫画或简单的矢量图形)并且在调整大小后变得模糊,那么它可以对 PNG 产生巨大的影响。

You may have to set the JPEG compression level. Currently it probably saves at a very high quality level, which may not be what you desire.

See here for more info: http://msdn.microsoft.com/en-us/library/bb882583.aspx

Note, however, that just reducing the resolution of the image doesn't necessarily reduce filesize. Due to how compression works, a smaller resolution file that has been blurred due to interpolation modes can be significantly larger than the original, though with JPEG that's probably not a big concern due to the lossy algorithm. It can make a huge difference for PNGs, though, if the original file was very simple before (like "flat" webcomic or simple vector graphics) and has been blurred after resizing.

人心善变 2024-11-14 14:08:04

无论文件大小问题如何,我绝对建议使用第一种方法,而不是使用 GetThumbnailImage 的方法。

GetThumbnailImage 将实际上从源图像中提取嵌入的缩略图(如果存在)。不幸的是,这意味着如果您没有预料到并考虑嵌入的缩略图,您就会从一些未知的原始文件大小和质量(与原始文件相比)进行缩放。这还意味着您在一次运行(带有嵌入缩略图)中获得的质量结果将与另一次运行(没有嵌入缩略图)中获得的质量结果截然不同。

我已经多次使用与您的第一种方法类似的方法,虽然我有时也看到过您所看到的情况,但结果始终更好。

Regardless of the file size issues, I would definitely recommend using the first method, rather than the one with the GetThumbnailImage.

GetThumbnailImage will actually pull an embedded thumbnail out of the source image if one exists. Unfortunately, this means you're scaling from some unknown original file size and quality (compared to the original) if you're not anticipating and taking into account the embedded thumbnail. It also means that you'll get very different quality results on one run (with an embedded thumbnail) than you would on another run (with no embedded thumbnail).

I've used something similar to your first method numerous times and while I have seen what you're seeing on occasion, the results were just consistently better.

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