1

我正在调整图像大小,bitmap.GetThumbnailImage()但我似乎正在失去图像质量/分辨率。例如原始图像是 300dpi 分辨率,而调整后的 1 则要小得多。

调整大小时如何保持图像分辨率?

4

2 回答 2

1

看看设置InterpolationMode(MSDN 链接)

您还应该查看此链接:创建高质量缩略图 - 动态调整图像大小

本质上,您的代码类似于以下内容: Bitmap imageToScale = new bitmap( // 使用要缩小的图像完成此操作

Bitmap bitmap = new Bitmap(imgWidth, imgHeight);  

using (Graphics graphics = Graphics.FromImage(result))
{
    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    graphics.DrawImageimageToScale, 0, 0, result.Width, result.Height);
    bitmap.Save(memoryStreamNew, System.Drawing.Imaging.ImageFormat.Png);
} 

bitmap.Save( //finish this depending on if you want to save to a file location, stream, etc...
于 2011-05-20T14:17:34.407 回答
1

这是我用来缩小图像的功能。质量非常好。

    private static Image ResizeImage(Image imgToResize, Size size)
    {
        int sourceWidth = imgToResize.Width;
        int sourceHeight = imgToResize.Height;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)size.Width / (float)sourceWidth);
        nPercentH = ((float)size.Height / (float)sourceHeight);

        if (nPercentH < nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();

        return (Image)b;
    }

这是我如何使用它:

        int length = (int)stream.Length;
        byte[] tempImage = new byte[length];
        stream.Read(tempImage, 0, length);

        var image = new Bitmap(stream);
        var resizedImage = ResizeImage(image, new Size(300, 300));

如果您需要帮助使其运行,请大声喊叫。

于 2011-05-20T17:42:37.437 回答