上传图片抛出异常(内存不足)
代码: public string UpLoadPhoto(string serPath, string fullName, int width, int height)
{
System.Drawing.Image oldImage, newImage;
System.Drawing.Image.GetThumbnailImageAbort call = new System.Drawing.Image.GetThumbnailImageAbort(CallBack);
oldImage = System.Drawing.Image.FromFile(fullName);
int nWidth, nHeight;
if (oldImage.Width > width || oldImage.Height > height)
{
nWidth = oldImage.Width;
nHeight = oldImage.Height;
while (nWidth > width)
{
int i = 100;
i = i - 1;
nWidth = nWidth * i / 100;
nHeight = nHeight * i / 100;
}
while (nHeight > height)
{
int i = 100;
i = i - 1;
nHeight = nHeight * i / 100;
nWidth = nWidth * i / 100;
}
}
else
{
nWidth = oldImage.Width;
nHeight = oldImage.Height;
}
newImage = oldImage.GetThumbnailImage(nWidth, nHeight, call, new IntPtr());
string fileName = System.IO.Path.GetFileName(fullName);
string expendName = System.IO.Path.GetExtension(fileName);
string newName = DateTime.Now.ToString().Replace( " ", " ").Replace( ": ", " ").Replace( "- ", " ").Replace( "/ ", " ");
if (expendName == ".gif "||expendName== ".GIF "||expendName== ".Gif ")
{
newImage.Save(serPath + newName + expendName, System.Drawing.Imaging.ImageFormat.Gif);
}
else
{
newImage.Save(serPath + newName + expendName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
oldImage.Dispose();
newImage.Dispose();
return newName + expendName;
}
public bool CallBack()
{
return false;
}
异常提示:内存不足。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.OutOfMemoryException: 内存不足。
源错误:
行 48: nHeight = oldImage.Height;
行 49: }
行 50: newImage = oldImage.GetThumbnailImage(nWidth, nHeight, call, new IntPtr());
行 51: string fileName = System.IO.Path.GetFileName(fullName);
行 52: string expendName = System.IO.Path.GetExtension(fileName);
源文件: d:\wwwroot\waawee\waawee.com\App_Code\UpLoad.cs 行: 50
[解决办法]
长度或者宽度是不是有个计算为0了?