我有一个文件上传控件,允许用户上传图像,但在他们上传图像之前,我想将 thomse 图像调整为 640x480 大小,问题是我不知道下一步该做什么。这就是我所拥有的;
// CALL THE FUNCTION THAT WILL RESIZE THE IMAGE
protected void btnUploadFile_Click(object sender, EventArgs e)
{
Stream imgStream = ir.ResizeFromStream(640, fupItemImage.PostedFile.InputStream);
// What to do next?
}
// THE FUNCTION THAT WILL RESIZE IMAGE THEN RETURN AS MEMORY STREAM
public MemoryStream ResizeFromStream(int MaxSideSize, Stream Buffer)
{
int intNewWidth;
int intNewHeight;
System.Drawing.Image imgInput = System.Drawing.Image.FromStream(Buffer);
// GET IMAGE FORMAT
ImageFormat fmtImageFormat = imgInput.RawFormat;
// GET ORIGINAL WIDTH AND HEIGHT
int intOldWidth = imgInput.Width;
int intOldHeight = imgInput.Height;
// IS 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 STREAM
MemoryStream imgMStream = new MemoryStream();
bmpResized.Save(imgMStream, imgInput.RawFormat);
// RELEASE RESOURCES
imgInput.Dispose();
bmpResized.Dispose();
Buffer.Close();
return imgMStream;
}
谢谢