我必须调整目录的图像大小,
但我在这条线上收到错误,访问被拒绝
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;
}
}