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

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

How can I delete or delete this stream so that I can work with the file?

+3
source share
2 answers

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

    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")
+2
source

FromStream has the exact same set of problems β€” the stream must remain open while the Image object exists. You must copy the image and then work on the copy. http://support.microsoft.com/kb/814675

+2

Source: https://habr.com/ru/post/1766707/


All Articles