Resizing an Image in .NETCF

I’ve seen some people ask how to resize an image or how to stretch an image in .NET Compact Framework in the community. Although the operation is pretty simple, I’ll share a code snippet anyway.

public static Image Resize(Image image, Size size)
{
    Image bmp = new Bitmap(size.Width, size.Height);
    using (var g = Graphics.FromImage(bmp))
    {
        g.DrawImage(
            image,
            new Rectangle(0, 0, size.Width, size.Height),
            new Rectangle(0, 0, image.Width, image.Height),
            GraphicsUnit.Pixel);
    }
    return bmp;
}

What the code above does is to create a new image with the specified new size and draw the source image to fit the new image.

Leave a Reply

Your email address will not be published. Required fields are marked *