缩小图像尺寸,但保持最高质量/分辨率?

时间:2011-05-20 13:43:09

标签: gdi+ asp.net-3.5 resolution image-resizing high-resolution

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

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

2 个答案:

答案 0 :(得分:1)

查看设置InterpolationMode(MSDN链接)

您还应该看一下这个链接:Create High Quality Thumbnail - Resize Image Dynamically

基本上,您的代码与以下内容类似: 位图imageToScale =新位图(//使用要减少的图像完成此操作

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

答案 1 :(得分: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));

Holler,如果你需要帮助让它运行。