Welcome, guest | Sign In | My Account | Store | Cart

Resize a GDI+ image using C#

Java, 16 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
private static Bitmap ResizeImage(Image image, int width, int height)
{
    Bitmap result = new Bitmap(width, height);

    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.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

        graphics.DrawImage(image, 0, 0, result.Width, result.Height);
    }

    return result;
}

Resizes with the best possible quality in GDI+, but also the slowest. Parameters can be tweaked to alter the quality/speed tradeoff.