-2

我必须调整目录的图像大小,

但我在这条线上收到错误,访问被拒绝

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;
        }
    }
4

3 回答 3

1

首先,您不必在保存之前删除文件,函数 Image.Save() 将替换现有图像,但您必须确保处理指向图像的所有句柄。这是一个例子来解释我的观点:

      var image = new Bitmap("test.png");

      var image2 = resizeImage(image, new Size(48, 48));
      //image.Dispose();
      image2.Save("test.png");

没有 dipose 你会有一个例外,但如果你取消注释行 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;
  }

}
于 2013-07-16T10:00:30.383 回答
1

也调用Dispose您的 Graphics 对象。

于 2013-07-16T10:10:56.937 回答
0

在尝试删除原始图像之前调用 Dispose。

于 2013-07-16T09:49:53.533 回答