将上传的图片通过Graphis缩小后保存,删除时却提示“该进程无法访问文件,因为该文件正由另一进程使用。”,百思不得其解,望各位高人指
代码如下:
//读取图片。
Image objSourceImage = Image.FromStream( objHttpPostedFile.InputStream );
//按原比例计算缩小图的宽和高。
int NewImageBaseLength = 400;
int NewImageWidth,NewImageHeight;
if ( objSourceImage.Width > objSourceImage.Height )
{
//当图片为横的时。
NewImageWidth = NewImageBaseLength;
NewImageHeight = ( int )( ( ( double )NewImageWidth / objSourceImage.Width ) * objSourceImage.Height );
}
else
{
//当图片为竖的时。
NewImageHeight = NewImageBaseLength;
NewImageWidth = ( int )( ( ( double )NewImageHeight / objSourceImage.Height ) * objSourceImage.Width );
}
//缩小图片。
Image objDestImage = new Bitmap( NewImageWidth , NewImageHeight );
Graphics objGraphics = Graphics.FromImage( objDestImage );
objGraphics.InterpolationMode = InterpolationMode.High;
objGraphics.SmoothingMode = SmoothingMode.HighQuality;
objGraphics.Clear( Color.Transparent );
objGraphics.DrawImage( objSourceImage , new Rectangle( 0 , 0 , objDestImage.Width , objDestImage.Height ) , new Rectangle( 0 , 0 , objSourceImage.Width , objSourceImage.Height ) , GraphicsUnit.Pixel );
//保存到指定目录。
objDestImage.Save( HttpContext.Current.Server.MapPath( objConfigInfo.UploadImagePath ) + objPicture.FileName , ImageFormat.Jpeg );
objGraphics.Dispose();
因为需要,要将上传的图片按比例缩小后保存在硬盘上,当需要删除的时候就出现“该进程无法访问文件,因为该文件正由另一进程使用。”,删除时使用的是File.Delete()方法,求各位高人指点一二。