Load Image From File and Release File

TnTinMN 0 Tallied Votes 6K Views Share

I have just seen a resurrected thread about issues with loading images to a PictureBox and the inability to delete the file while the program is running. There was no clear explaination of the problem, only suggested hacks and apparent frustration.

So I thought I would offer this explanation from Microsoft:

When either a Bitmap object or an Image object is constructed from a file, the file remains locked for the lifetime of the object. As a result, you cannot change an image and save it back to the same file where it originated.

Additionally, if the stream was destroyed during the life of the Bitmap object, you cannot successfully access an image that was based on a stream. For example, the Graphics.DrawImage() function may not succeed after the stream has been destroyed.

GDI+, and therefore the System.Drawing namespace, may defer the decoding of raw image bits until the bits are required by the image. Additionally, even after the image has been decoded, GDI+ may determine that it is more efficient to discard the memory for a large Bitmap and to re-decode later. Therefore, GDI+ must have access to the source bits for the image for the life of the Bitmap or the Image object.

To retain access to the source bits, GDI+ locks any source file, and forces the application to maintain the life of any source stream, for the life of the Bitmap or the Image object.

To make a long story short for their suggested workarounds, I have created the function shown to return an image and not lock the file.

example usage: PictureBox1.Image = GetImageFile("KeepGDIPlusFromLockingMe.jpg")

Public Function GetImageFile(ByVal fn As String) As Image
         If IO.File.Exists(fn) Then
            Dim img As Image = Image.FromFile(fn)
            Dim bm As New Bitmap(Width:=img.Width, Height:=img.Height, Format:=img.PixelFormat)
            Using g As Graphics = Graphics.FromImage(bm)
               g.DrawImage(img, Point.Empty)
            End Using
            Return bm
            MessageBox.Show("Nice try!, but " & fn & "does not exist.  Returning Nothing")
            Return Nothing
         End If
      End Function
Jo_2 0 Newbie Poster

thank you its work for me :)

J.C. SolvoTerra 109 Eat, Sleep, Code, Repeat Featured Poster

Hi Jo 2,

I've just spotted this, and thought I'd point out a much simpler way of doing this

'Load your image file, essentially locking access to it
Dim BMP As Bitmap = Bitmap.FromFile("C:\Users\Jay\Desktop\test.jpg")

'When assigning to your picture box, create a new bitmap
PictureBox1.Image = New Bitmap(BMP)

'Dispose of the original bitmap, freeing up access to the file

'Speed up garbage collection, releasing your file.

I hope this helps.

Marcelo L. commented: You are like a Ninja. I passed days serching for that simple line: gc.colllect(). Magic! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.