-2

i have to resize image of a directory,

but i am getting error, access denied, on this line

System.IO.File.Delete(_path);

i know it is because the image i read is in used, but dont know how to handle it

I have found links on SO, but all are in 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);
    }
}

code of resize

 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;
        }
    }
Your father
  • 97
  • 1
  • 11

3 Answers3

1

First you don't have to delete the file before save, the function Image.Save() will replace the existing image but you have to make sure to dispose all handles pointing to the image. here is an example to explain my point:

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

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

Without the dipose you will have an exeption, but if you uncomment the line image.Dispose() it will work like a charm.

for the function resizeImage I copied it for the question Resize an Image C#

and here is the code:

public static Image resizeImage(Image imgToResize, Size size)
{
  return (Image)(new Bitmap(imgToResize, size));
}

EDIT:

It seems that the Graphics object is the reason, you have to call oGraphic.Dispose();

Call the _productImage.Dispose() after the save.

In fact a more proper way is :

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

}
Community
  • 1
  • 1
Swift
  • 1,861
  • 14
  • 17
1

Call Dispose on your Graphics object as well.

dotNET
  • 33,414
  • 24
  • 162
  • 251
0

Call Dispose on your original image before trying to delete it.

Tamim Al Manaseer
  • 3,554
  • 3
  • 24
  • 33