/ Published in: C#
Image Upload and Resize
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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; i ntNewWidth = Convert.ToInt32(dblCoef * intOldWidth); intNewHeight = Convert.ToInt32(dblCoef * intOldHeight); } else { intNewWidth = intOldWidth; intNewHeight = intOldHeight; } //create new bitmap //save bitmap to disk bmpResized.Save(ImageSavePath, fmtImageFormat); //release used resources imgInput.Dispose(); bmpResized.Dispose(); Buffer.Close(); }