Search code examples
gdi+system.drawing

System.Drawing.Image.FromFile does not close the file stream


If i create an image with this method and then try to delete/modify the image file I get an error because there is a stream still using the file.

How can I delete or dispose of this stream so that I can work on the file?


Solution

  • Do not use this method. Use FromStream instead and do this:

        Using FileStream = New IO.FileStream("D:\Test.jpg", IO.FileMode.Open)
            Dim x = System.Drawing.Image.FromStream(FileStream)
    
            'Do your image manipulation...'
        End Using
    
        IO.File.Delete("D:\Test.jpg")