locked
How to open an image and save it as the same filename RRS feed

  • Question

  • User-29703693 posted

    I need to open an image, then crop it and save it as the same filename.  I keep getting the error - "A generic error occurred in GDI+" bu this only appears when I try saving it as the same filename.  Here's the code:

             Dim i As Image = Image.FromFile("C:\inetpub\wwwroot\crop\image.jpg")
            Dim t As Image = New Bitmap(i.Width, i.Height)

            Dim g As Graphics = Graphics.FromImage(t)
            g.DrawImage(i, 0, 0)
            i.Dispose()
            g.Dispose()

            Dim filepath As String = "C:\inetpub\wwwroot\crop\image.jpg"
            t.Save(filepath, ImageFormat.Jpeg)
     

     

    NOTE: if I change the filepath to anything other than image.jpg it works fine.  However, it seems like the original image is still stuck in memory even though I have i.Dispose().

     Any help is greatly appreciated!  Thanks!

    Wednesday, March 4, 2009 9:25 AM

Answers

  • User-29703693 posted

     It's working now.  I think the original code was fine but the image had been locked in memory from one of my previous tests.  I also added t.Dispose() at the end of the code.  Not sure if that helps or not.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 5, 2009 9:08 AM

All replies

  • User-1643231461 posted

    quoted from MSDN

    Saving the image to the same file it was constructed from is not allowed and throws an exception.

    I would try saving to a different file, and then use System.IO to delete the original and rename the new file to the original file name.

    Wednesday, March 4, 2009 9:59 AM
  • User-29703693 posted

     

    quoted from MSDN

    Saving the image to the same file it was constructed from is not allowed and throws an exception.

    I would try saving to a different file, and then use System.IO to delete the original and rename the new file to the original file name.

     

    Thanks for the response.  When I try deleting the original I get the error:

    Access to the path 'C:\inetpub\wwwroot\crop\image.jpg' is denied.

     

    For some reason it seems the image is not getting properly disposed and it's staying in memory.  Any ideas? 

     

    Wednesday, March 4, 2009 12:08 PM
  • User-1643231461 posted

    I'm not too familiar with this Image class. However maybe you could create a copy of the file and load and manipulate that, then save it to the original file. Beyond that, i don't have any other ideas, and i hope someone can help you figure this out

    Wednesday, March 4, 2009 9:55 PM
  • User-29703693 posted

     It's working now.  I think the original code was fine but the image had been locked in memory from one of my previous tests.  I also added t.Dispose() at the end of the code.  Not sure if that helps or not.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 5, 2009 9:08 AM
  • User-1643231461 posted

    Wonderful! I have that happen all the time. If i'm getting errors and i think that could be the problem i usually head over to windows explorer and try to delete the file. If its locked it gives an error stating you can't delete it. thats a quick way to check without doing some fancy debugging.

     Also Since you have fixed the problem could you mark this thread as resolved and your post stating the fix as the answer?

    Monday, March 16, 2009 4:04 PM