我必须调整目录图像的大小,
但我收到错误,拒绝访问, 在这一行
System.IO.File.Delete(_path);
我知道这是因为我阅读的图像已被使用,但不知道如何处理它
我在SO上找到了链接,但都在php中: - (
for (int i = 1; i < dt.Rows.Count; i++)
{
try
{
string _ImageUrl = HttpContext.Current.Request.PhysicalApplicationPath +
"Data\\" + dt.Rows[i]["ProductImage"].ToString();
string _extName = System.IO.Path.GetExtension(_ImageUrl);
System.Drawing.Image _productImage = ImageReSize(_ImageUrl, 500);
string _path = _ImageUrl;
if (System.IO.File.Exists(_path))
System.IO.File.Delete(_path);
_productImage.Save(_path);
_productImage = ImageReSize(_ImageUrl, 85);
string _strImageName2 = dt.Rows[i]["ProductSmallImage"].ToString();
_path = HttpContext.Current.Request.PhysicalApplicationPath +
"Data\\" + _strImageName2;
if (System.IO.File.Exists(_path))
System.IO.File.Delete(_path);
_productImage.Save(_path);
}
}
调整大小的代码
public System.Drawing.Image ImageReSize(string _imageUrl, int Width)
{
try
{
//uploadImageFile.PostedFile.InputSteam
System.Drawing.Image oImg = System.Drawing.Image.FromFile(_imageUrl);
//((oh *nw) / ow)*100
int Height = ((oImg.Height * Width) / oImg.Width); // (oImg.Width * Width);
Size PictureThumbSize = new Size();
PictureThumbSize.Height = Height;
PictureThumbSize.Width = Width;
System.Drawing.Image oThumbNail = new Bitmap(PictureThumbSize.Width, PictureThumbSize.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics oGraphic = Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality;
oGraphic.SmoothingMode = SmoothingMode.HighQuality;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle oRectangle = new Rectangle(0, 0, PictureThumbSize.Width, PictureThumbSize.Height);
oGraphic.DrawImage(oImg, oRectangle);
oImg.Dispose();
return oThumbNail;
//oThumbNail.Save(sPhysicalPath + @"\" + newFileName, ImageFormat.Jpeg);
}
catch (Exception Ex)
{
return null;
}
}
答案 0 :(得分:1)
首先,您不必在保存之前删除文件,Image.Save()函数将替换现有图像,但您必须确保处置指向图像的所有句柄。 这是一个解释我的观点的例子:
var image = new Bitmap("test.png");
var image2 = resizeImage(image, new Size(48, 48));
//image.Dispose();
image2.Save("test.png");
如果没有dipose,你将有一个exeption,但如果你取消注释行image.Dispose()它将像魅力一样工作。
对于函数resizeImage我将其复制为问题Resize an Image C#
这是代码:
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
修改强>
看来Graphics对象是原因,你必须调用oGraphic.Dispose();
保存后调用_productImage.Dispose()。
实际上更合适的方式是:
for (int i = 1; i < dt.Rows.Count; i++)
{
try
{
string _ImageUrl = HttpContext.Current.Request.PhysicalApplicationPath +
"Data\\" + dt.Rows[i]["ProductImage"].ToString();
string _extName = System.IO.Path.GetExtension(_ImageUrl);
using (System.Drawing.Image _productImage = ImageReSize(_ImageUrl, 500))
{
string _path = _ImageUrl;
if (System.IO.File.Exists(_path))
System.IO.File.Delete(_path);
_productImage.Save(_path);
}
using (System.Drawing.Image _productImage = ImageReSize(_ImageUrl, 85))
{
string _strImageName2 = dt.Rows[i]["ProductSmallImage"].ToString();
_path = HttpContext.Current.Request.PhysicalApplicationPath +
"Data\\" + _strImageName2;
if (System.IO.File.Exists(_path))
System.IO.File.Delete(_path);
_productImage.Save(_path);
}
}
catch
{
//Handle the exeption
}
}
public System.Drawing.Image ImageReSize(string _imageUrl, int Width)
{
try
{
//uploadImageFile.PostedFile.InputSteam
using (System.Drawing.Image oImg = System.Drawing.Image.FromFile(_imageUrl))
//((oh *nw) / ow)*100
{
int Height = ((oImg.Height * Width) / oImg.Width); // (oImg.Width * Width);
Size PictureThumbSize = new Size();
PictureThumbSize.Height = Height;
PictureThumbSize.Width = Width;
System.Drawing.Image oThumbNail = new Bitmap(PictureThumbSize.Width, PictureThumbSize.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics oGraphic = Graphics.FromImage(oThumbNail))
{
oGraphic.CompositingQuality = CompositingQuality.HighQuality;
oGraphic.SmoothingMode = SmoothingMode.HighQuality;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle oRectangle = new Rectangle(0, 0, PictureThumbSize.Width, PictureThumbSize.Height);
oGraphic.DrawImage(oImg, oRectangle);
}
return oThumbNail;
}
//oThumbNail.Save(sPhysicalPath + @"\" + newFileName, ImageFormat.Jpeg);
}
catch (Exception Ex)
{
return null;
}
}
答案 1 :(得分:1)
也可以在Graphics对象上调用Dispose
。
答案 2 :(得分:0)
在尝试删除原始图像之前调用Dispose。