今天在做一个图片删除时遇到些问题,删除图片时总是报"该进程无法访问文件“XXXX”,因为该文件正由另一进程使用。",很明显是资源没释放的原因.
private void GetThumbNail(string strFileName, int iWidth, int iheight)

{

System.Drawing.Bitmap oImg;

float tmpRa1,tmpRa2;
oImg = new Bitmap(Server.MapPath(strFileName));
tmpRa1 = (float)oImg.Width/(float)iWidth;
tmpRa2 = (float)oImg.Height/(float)iheight;
if(tmpRa1>tmpRa2)

{
iheight = (int)(((float)oImg.Height)/tmpRa1);
}
else

{
iWidth = (int)(((float)oImg.Width)/tmpRa2);
}

oImg = ImgResize(oImg,iWidth,iheight);
Response.ContentType = "image/jpeg";
MemoryStream MemStream = new MemoryStream();
oImg.Save(MemStream,System.Drawing.Imaging.ImageFormat.Jpeg);
MemStream.WriteTo(Response.OutputStream);
oImg.Dispose();
MemStream.Close();
}

private System.Drawing.Bitmap ImgResize(Bitmap objImage,int iWidth,int iHeight)

{
Bitmap newImg;
Graphics g;
newImg=new Bitmap(iWidth,iHeight,PixelFormat.Format24bppRgb);
Rectangle destRect = new Rectangle( 0,0,iWidth,iHeight);
g=Graphics.FromImage(newImg);
g.DrawImage(objImage,destRect);
//objImage.Dispose();
objImage = newImg;
return objImage;
}
上面我注释的那段加上就没问题了.
我原以为这个无引用的资源会自动释放呢.