GDI+ Error on Bitmap Save RRS feed

  • Question

  • User-1170096362 posted


    I have a helper class that has a method to resize images. It takes the virtual path of an image and resize it to a specified dimension like this.

      public void ResizeImageTest(string pathImage, int newDimension)
       string pathImageOnDisk = System.Web.HttpContext.Current.Server.MapPath(pathImage);
       System.IO.FileStream fs = new System.IO.FileStream(pathImageOnDisk, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
       Image imageOriginal = Image.FromStream(fs);

       using (Bitmap bitmapNewImage = new Bitmap(imageOriginal, newDimension, newDimension))
        bitmapNewImage.Save(pathImageOnDisk, imageOriginal.RawFormat);
    When I try to call this method, it will error out in the line of the "bitmapNewImage.Save" with this:

      Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
    However, it works fine if I save to a different file like this:
      string pathDummyImageOnDisk = System.Web.HttpContext.Current.Server.MapPath("/images/dummyPic.jpg");
      bitmapNewImage.Save(pathDummyImageOnDisk, imageOriginal.RawFormat);
    I have searched the web and found that my solution might be in here:

    But I don't really understand the steps, such as how to create a new bitmap "with a pixel format of more than 8 bits-per-pixel (BPP)", how to "use the Graphics.FromImage() method to obtain a Graphics object for the second Bitmap", use "Marshal.Copy function ... to copy the image bits from the first Bitmap to the second Bitmap" etc.

    I suppose I could save it as a dummy file, close the original and rename the dummy file back to the original's name, but I'm just thinking this isn't the best solution.

    Any advice?


    Friday, December 16, 2005 2:55 PM

All replies

  • User2128074059 posted
    Maybe you, I guess, have no popedom to access that folder.
    Saturday, December 17, 2005 3:21 AM
  • User-1170096362 posted
    I don't think it's a problem with the folder access permission. I can save as a dummy file in that folder.
    Saturday, December 17, 2005 10:34 AM
  • User2128074059 posted

    Can you dispose the image object?

    Maybe the Bitmap remains locked the object.

    Sunday, December 18, 2005 2:49 AM