Google Search

Google
 

Thursday, June 5, 2008

Resize Image

You may use this method to resize image and set the maximum side size in pixels when uploading.
You will need a reference to System.Drawing and System.Drawing.Imaging namespaces public void ResizeFromStream(string ImageSavePath, int MaxSideSize, Stream Buffer)
{
int intNewWidth; int intNewHeight; Image imgInput = Image.FromStream(Buffer); //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);
//save bitmap to disk
bmpResized.Save(ImageSavePath, fmtImageFormat);
//release used resources
imgInput.Dispose(); bmpResized.Dispose(); Buffer.Close();
}

No comments: