public static Bitmap ReSizeBitmap(Bitmap btp, int width, int height)
{
int originalW = btp.Width;
int originalH = btp.Height;
int proportionsW = 0;
int proportionsH = 0;

if ((originalH * width) / originalW <= height)
{
proportionsW = width;
proportionsH = (originalH * width) / originalW;
}
else
{
proportionsH = height;
proportionsW = (originalW * height) / originalH;
}

Bitmap bmp = new Bitmap(width, height);

//create a new graphic from the Bitmap
Graphics graphic = Graphics.FromImage((Image)bmp);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
//draw the newly resized image
graphic.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, width, height));
graphic.DrawImage(btp as Image, (width – proportionsW) / 2, (height – proportionsH) / 2, proportionsW, proportionsH);
graphic.Dispose();
//return the image
return bmp;
}

Advertisements