图像裁剪和调整大小 C# 中的 ImageMagick 与 GDI+

本文关键字:中的 ImageMagick GDI+ 裁剪 调整 图像 | 更新日期: 2023-09-27 18:36:47

我有一个Web应用程序,用户可以在其中上传图片来创建他们的画廊。几年前,当我编写应用程序时,我选择了ImageMagick,并使用ImageMagick进行了所有裁剪和调整大小。

现在我从头开始重写应用程序,我将 ImageMagick 替换为本机 GDI+ 操作,但我对 GDI+ 了解得越多,我就越害怕我做出了错误的选择。

我到处都读到GDI+适用于桌面,不应在服务器应用程序上使用。我不知道细节,但我想这是为了内存消耗,事实上我可以看到GDI+使用更多的内存对同一图像执行相同的操作(裁剪和调整大小)比ImageMagick(而老实说GDI+更快)。

我相信GDI+,ImageMagick或任何其他库对于这些基本操作应该或多或少相同,我喜欢使用本机GDI+的想法,相信MS随.NET一起提供的任何内容至少应该没问题。

使用什么正确的方法/工具?

这是我用来裁剪的代码:

internal Image Crop(Image image, Rectangle r)
{
    Bitmap bmpCrop;
    using (Bitmap bmpImage = new Bitmap(image))
    {
        bmpCrop = bmpImage.Clone(r, bmpImage.PixelFormat);
        bmpImage.Dispose();
    }
    return (Image)(bmpCrop);
}

这是我用来调整大小的代码:

internal Image ResizeTo(Image sourceImage, int width, int height)
{
    System.Drawing.Image newImage = new Bitmap(width, height);
    using (Graphics gr = Graphics.FromImage(newImage))
    {
        gr.SmoothingMode = SmoothingMode.AntiAlias;
        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
        gr.DrawImage(sourceImage, new Rectangle(0, 0, width, height));
        gr.Dispose();
    }
    return newImage;
}

图像裁剪和调整大小 C# 中的 ImageMagick 与 GDI+

你能链接到人们说不应该在服务器上使用 GDI+ 的地方吗?也许他们知道一些我不知道的事情。

我知道一些关于GDI+如何工作的事情,但对ImageMagick一无所知。我碰巧看到了这个描述ImageMagick架构的页面: http://www.imagemagick.org/script/architecture.php

似乎ImageMagick将在内部将图像转换为具有4个通道和特定位深度(通常为每通道16位)的未压缩格式,并使用未压缩的数据进行操作,这些数据可能位于内存中或磁盘上,具体取决于大小。"识别版本"将告诉您位深度是多少。我的印象是,在实践中,ImageMagick通常会在内部使用64位RGBA缓冲区,除非您使用使用32位RGBA的Q8版本。它也可以使用多个线程,但我怀疑这是否重要,除非您处理非常大的图像。(如果您正在处理非常大的图像,ImageMagick 显然是赢家。

GDI+ 位图对象将始终将未压缩数据存储在内存中,并且通常默认为 32 位 RGBA。那和32位RGB可能是最有效的格式。GDI+ 是一个绘图库,它不是为大图像设计的,但至少位图对象不会包含除像素数据和图像元数据的内存之外的任何资源(与流行的看法相反,它们不包含 HBITMAP 对象)。

所以他们看起来和我非常相似。对于您的用例,我不能说一个显然比另一个更好。如果你使用imagemagick,你可能应该使用Q8版本来增加速度和内存,除非额外的精度对你很重要。

似乎如果您唯一的操作是加载、保存、缩放和裁剪,那么如果需要,您应该能够在以后轻松替换实现。

除非需要使用图元文件,否则可能应该在内部使用位图对象,而不是图像。这样,就不必在 Crop 函数中创建中间位图对象。该中间对象可能是您观察到的一些额外内存消耗的背后原因。如果您从外部源获取 Image 对象,我建议尝试将它们投射到 Bitmap 并创建一个新的位图(如果这不起作用)。

此外,"using"语句会自动调用 Dispose,因此无需显式调用它。

我自己写了一些东西:

public void ResizeImageAndRatio(string origFileLocation, string newFileLocation, string origFileName, string newFileName, int newWidth, int newHeight, bool resizeIfWider)
{
    System.Drawing.Image initImage = System.Drawing.Image.FromFile(origFileLocation + origFileName);
    int templateWidth = newWidth;
    int templateHeight = newHeight;
    double templateRate = double.Parse(templateWidth.ToString()) / templateHeight;
    double initRate = double.Parse(initImage.Width.ToString()) / initImage.Height;
    if (templateRate == initRate)
    {
        System.Drawing.Image templateImage = new System.Drawing.Bitmap(templateWidth, templateHeight);
        System.Drawing.Graphics templateG = System.Drawing.Graphics.FromImage(templateImage);
        templateG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        templateG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        templateG.Clear(Color.White);
        templateG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, templateWidth, templateHeight), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);
        templateImage.Save(newFileLocation + newFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    else
    {
        System.Drawing.Image pickedImage = null;
        System.Drawing.Graphics pickedG = null;
        Rectangle fromR = new Rectangle(0, 0, 0, 0);
        Rectangle toR = new Rectangle(0, 0, 0, 0);
        if (templateRate > initRate)
        {
            pickedImage = new System.Drawing.Bitmap(initImage.Width, int.Parse(Math.Floor(initImage.Width / templateRate).ToString()));
            pickedG = System.Drawing.Graphics.FromImage(pickedImage);
            fromR.X = 0;
            fromR.Y = int.Parse(Math.Floor((initImage.Height - initImage.Width / templateRate) / 2).ToString());
            fromR.Width = initImage.Width;
            fromR.Height = int.Parse(Math.Floor(initImage.Width / templateRate).ToString());
            toR.X = 0;
            toR.Y = 0;
            toR.Width = initImage.Width;
            toR.Height = int.Parse(Math.Floor(initImage.Width / templateRate).ToString());
        }
        else
        {
            pickedImage = new System.Drawing.Bitmap(int.Parse(Math.Floor(initImage.Height * templateRate).ToString()), initImage.Height);
            pickedG = System.Drawing.Graphics.FromImage(pickedImage);
            fromR.X = int.Parse(Math.Floor((initImage.Width - initImage.Height * templateRate) / 2).ToString());
            fromR.Y = 0;
            fromR.Width = int.Parse(Math.Floor(initImage.Height * templateRate).ToString());
            fromR.Height = initImage.Height;
            toR.X = 0;
            toR.Y = 0;
            toR.Width = int.Parse(Math.Floor(initImage.Height * templateRate).ToString());
            toR.Height = initImage.Height;
        }
        pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
        System.Drawing.Image templateImage = new System.Drawing.Bitmap(templateWidth, templateHeight);
        System.Drawing.Graphics templateG = System.Drawing.Graphics.FromImage(templateImage);
        templateG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        templateG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        templateG.Clear(Color.White);
        templateG.DrawImage(pickedImage, new System.Drawing.Rectangle(0, 0, templateWidth, templateHeight), new System.Drawing.Rectangle(0, 0, pickedImage.Width, pickedImage.Height), System.Drawing.GraphicsUnit.Pixel);
        templateImage.Save(newFileLocation + newFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        templateG.Dispose();
        templateImage.Dispose();
        pickedG.Dispose();
        pickedImage.Dispose();
    }
    initImage.Dispose();
}