C# 多次保存文件失败,GDI+中出现一般错误。例外
下面是违规代码。该流来自HttpPostedFile。在本地测试是可以的。 在线测试也可以在第一次覆盖时使用。只有在多次尝试覆盖文件时才会引发异常。上传然后重新上传任何见解C# 多次保存文件失败,GDI+中出现一般错误。例外,c#,.net,gdi,C#,.net,Gdi,下面是违规代码。该流来自HttpPostedFile。在本地测试是可以的。 在线测试也可以在第一次覆盖时使用。只有在多次尝试覆盖文件时才会引发异常。上传然后重新上传任何见解 public void ResizeFromStream(string ImageSavePath, int MaxSideSize, Stream Buffer) { int intNewWidth; int intNewHeight; System.D
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();
}
该文件可能已被锁定,因为有人开始下载它。在保存之前尝试删除文件-如果我是对的,它也会失败。谢谢。不完全正确,但它让我走上了正确的道路。事实证明,在这个函数调用之后,我有另一个函数打开文件,但从未处理过资源,因此导致第二次调用失败。奇怪的是,完全相同的代码在本地运行时没有任何问题……嗯,的确如此。服务器往往更挑剔,完整的IIS比IIS Express更严格。这就是为什么您需要一个尽可能靠近生产配置的临时区域:这里还有另一个教训-在有意义的时候尝试使用,然后尝试。。。最后,当您释放任何资源时。它有助于构建可靠的应用程序: