以下是有问题的代码。 Stream来自HttpPostedFile。在当地测试很好。 在线测试也是第一次覆盖时没问题。只有在尝试多次覆盖文件时才会抛出异常。例如。上传然后重新上传任何见解?
public void ResizeFromStream(string ImageSavePath, int MaxSideSize, Stream Buffer)
{
int intNewWidth;
int intNewHeight;
System.Drawing.Image imgInput = System.Drawing.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;
}
Bitmap bmpResized = null;
if (intMaxSide > MaxSideSize)
{
//set new width and height
double dblCoef = MaxSideSize / (double)intMaxSide;
intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
bmpResized = new Bitmap(imgInput, intNewWidth, intNewHeight);
}
else
{
intNewWidth = intOldWidth;
intNewHeight = intOldHeight;
bmpResized = new Bitmap(Buffer);
}
//save bitmap to disk
bmpResized.Save(ImageSavePath, fmtImageFormat);
//release used resources
imgInput.Dispose();
bmpResized.Dispose();
Buffer.Dispose();
}