Cropping an Image with best quality using C#

Hi all,

Here is the sample code for getting cropped image with the best quality. Try it out.


public Image CropImage(Image img, int left, int top, int width, int height)
{

try

{

Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);

bmp.SetResolution(80, 60);

Graphics graphics = Graphics.FromImage(bmp);

graphics.SmoothingMode = SmoothingMode.AntiAlias;

graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

graphics.DrawImage(img, new Rectangle(0, 0, width, height), left, top, width, height, GraphicsUnit.Pixel);

img.Dispose();

bmp.Dispose();

graphics.Dispose();

return bmp;
}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

return null;

}
}


that's it....

...S.VinothkumaR.

No comments: