Friday, September 5, 2008

Resize Image C#

public void ResizeFromStream(string sourFilePath, int MaxSideSize, string destFilePath)
{
System.Drawing.Image imgInput = System.Drawing.Image.FromFile(sourFilePath);
int intNewWidth;
int intNewHeight;

//Determine image format
ImageFormat fmtImageFormat = imgInput.RawFormat;

//get image original width and height
int intOldWidth = imgInput.Width;
int intOldHeight = imgInput.Height;

//determine if landscape or portrait
int intMaxSide;

if (intOldWidth >= intOldHeight)
{
intMaxSide = intOldWidth;
}
else
{
intMaxSide = intOldHeight;
}
if (intMaxSide > MaxSideSize)
{
//set new width and height
double dblCoef = MaxSideSize / (double)intMaxSide;
intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
}
else
{
intNewWidth = intOldWidth;
intNewHeight = intOldHeight;
}
//create new bitmap
Bitmap bmpResized = new Bitmap(imgInput, intNewWidth, intNewHeight);

bmpResized.Save(destFilePath, ImageFormat.Jpeg);
}

No comments: