Categories
c# image image-scaling

Resizing an Image without losing any quality [closed]

112

How can I resize an image, with the image quality unaffected?

3

32

Unless you’re doing vector graphics, there’s no way to resize an image without potentially losing some image quality.

3

  • 1

    unless you’re expanding it…

    Sep 17, 2008 at 21:18

  • You can expand it without losing any information, but there are different types of filters you can use which give different results – zero-order hold, low-pass, etc.

    Sep 17, 2008 at 21:21

  • Can confirm, I dramatically said “Enhance!” to my computer and it didn’t work.

    – SteveCav

    Jan 21, 2021 at 4:42

26

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;
}

from here

1

  • This works, but returns identical quality to Kris Erickso’s answer. Nice to see Size used though…

    – Sam Jones

    Aug 8, 2013 at 9:35