调整大小,质量损失最小

时间:2010-04-21 19:22:34

标签: c# image

我需要调整图像大小,但图像质量不受此影响,图像将从10x10到​​1000x1000,它会有一些主要的拥塞和一些空的时间 它必须向上和向下扩展“可能会失去一些图像质量。”没关系,但它必须至少,一切都有光栅图形确实 请不要使用库或其他外部二进制文件

3 个答案:

答案 0 :(得分:4)

以下是显示如何调整图片大小的链接:

http://snippets.dzone.com/posts/show/4336

答案 1 :(得分:0)

无论何时调整图像大小,都会导致某些质量下降 - 没有办法解决问题,但您可以通过在图形上下文中使用最高质量的选项来帮助最小化它。 / p>

这是我使用普通GDI +函​​数的方法:

public static Bitmap ResizeImage(Bitmap bmp, int width, int height, 
                                 InterpolationMode mode = InterpolationMode.HighQualityBicubic)                                         
{
    Bitmap bmpOut = null;

    try
    {                                
        decimal ratio;
        int newWidth = 0;
        int newHeight = 0;

        // If the image is smaller than a thumbnail just return original size
        if (bmp.Width < width && bmp.Height < height)
        {
            newWidth = bmp.Width;
            newHeight = bmp.Height;
        }
        else
        {
            if (bmp.Width == bmp.Height)
            {
                if (height > width)
                {
                    newHeight = height;
                    newWidth = height;
                }
                else
                {
                    newHeight = width;
                    newWidth = width;                         
                }
            }
            else if (bmp.Width >= bmp.Height)
            {
                ratio = (decimal) width/bmp.Width;
                newWidth = width;
                decimal lnTemp = bmp.Height*ratio;
                newHeight = (int) lnTemp;
            }
            else
            {
                ratio = (decimal) height/bmp.Height;
                newHeight = height;
                decimal lnTemp = bmp.Width*ratio;
                newWidth = (int) lnTemp;
            }
        }

        //bmpOut = new Bitmap(bmp, new Size( newWidth, newHeight));                     
        bmpOut = new Bitmap(newWidth, newHeight);                
        bmpOut.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);

        Graphics g = Graphics.FromImage(bmpOut);
        g.InterpolationMode = mode;

        g.SmoothingMode = SmoothingMode.HighQuality;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;

        g.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
        g.DrawImage(bmp, 0, 0, newWidth, newHeight);                
    }
    catch
    {
        return null;
    }

    return bmpOut;
}

将图像调整为宽度或高度中的最大值 - 您可以改变该算法以适应调整大小的需要。

如果您想使用文件,可以添加另一个包装上述代码的帮助程序:

public static bool ResizeImage(string filename, string outputFilename, 
                                int width, int height, 
                                InterpolationMode mode = InterpolationMode.HighQualityBicubic)
{

    using (var bmpOut = ResizeImage(filename, width, height, mode) )
    {
        var imageFormat = GetImageFormatFromFilename(filename);
        if (imageFormat == ImageFormat.Emf)
            imageFormat = bmpOut.RawFormat; 

        bmpOut.Save(outputFilename, imageFormat);
    }

    return true;
}

GDI +通常不是高质量图像处理的最佳解决方案 - 它很不错但是如果您需要Web应用程序中的最高质量,更好性能和线程安全性,则需要查看其他选项。

Bertrand LeRoy在前一段时间有一篇关于Image Resizing的文章,它提供了一些使用核心.NET框架的替代方案。 http://weblogs.asp.net/bleroy/state-of-net-image-resizing-how-does-imageresizer-do

答案 2 :(得分:-2)

您需要的是图像可扩展。我建议使用XML格式来存储图像,因为XML具有很高的可扩展性。例如:

<Image Width="640" Height="480">
  <Pixel X="0" Y="0">
    <Red>20</Red>
    <Green>80</Green>
    <Blue>0</Blue>
    <Alpha>255</Alpha>
  </Pixel>
  <Pixel X="1" Y="0">
    ...

如果你担心记忆,我打赌这会很好地压缩: - )。

相关问题