Crop and Resize Image to Specific Dimensions

Posted by Crunchy on Stack Overflow See other posts from Stack Overflow or by Crunchy
Published on 2011-01-08T23:07:17Z Indexed on 2011/01/08 23:54 UTC
Read the original article Hit count: 638

Filed under:
|

I need some help cropping and resizing images using CSharp.net. My goal here is to take an image and reduce it to 50px by 50px. The following code I found here will do that, however it's scaling the image as well. Ideally I want to scale the image down to as close to 50px by 50px as possible and then remove the parts of the image that are outside 50px by 50px.

public Image ResizeImage(Image img, int width, int height)
{
    Bitmap b = new Bitmap(width, height);
    using (Graphics g = Graphics.FromImage((Image)b))
    {
        g.DrawImage(img, 0, 0, width, height);
    }

    return (Image)b;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about resize-image