ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

ScaleImage

Scales a Image to make it fit inside of a Height/Width

Source

public static Image ScaleImage(this Image img, int height, int width)
{
    if (img == null || height <= 0 || width <= 0)
    {
        return null;
    }
    int newWidth = (img.Width * height) / (img.Height);
    int newHeight = (img.Height * width) / (img.Width);
    int x = 0;
    int y = 0;

    Bitmap bmp = new Bitmap(width, height);
    Graphics g = Graphics.FromImage(bmp);
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;

    // use this when debugging.
    //g.FillRectangle(Brushes.Aqua, 0, 0, bmp.Width - 1, bmp.Height - 1);
    if (newWidth > width)
    {
        // use new height
        x = (bmp.Width - width) / 2;
        y = (bmp.Height - newHeight) / 2;
        g.DrawImage(img, x, y, width, newHeight);
    }
    else
    {
        // use new width
        x = (bmp.Width / 2) - (newWidth / 2);
        y = (bmp.Height / 2) - (height / 2);
        g.DrawImage(img, x, y, newWidth, height);
    }
    // use this when debugging.
    //g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, bmp.Width - 1, bmp.Height - 1);
    return bmp;
}

Example

Image test = someImg.ScaleImage(591, 1096);

Author: Robert Booth

Submitted on: 30 mrt 2009

Language: C#

Type: System.Image

Views: 7746