When I recently read the program, I found that when I used picturebox images to display images, there will be "system. Drawing. image. fromfile <system. outofmemoryexception> insufficient memory .」 . Google, and someone found that they do not use image. fromfile, so log it here.
Assign provides picturebox. Image mirroring in the following ways:
Method 1: Use image. fromfile
Me.PictureBox1.Image = Image.FromFile("f:\img1.jpg")
Method 2: Use image. fromstream
Dim fs As System.IO.FileStream = System.IO.File.OpenRead("f:\img1.jpg")
Me. picturebox1.image = image. fromstream (FS)
fs.Close()
It is recommended that method 2 be used because method 1 is used. After assign is complete, the image watermark will still be locked, but method 2 won't.
You can use the following methods to visualize picturebox. Image,
Method 1: directly use picturebox. Image
If Not (Me.PictureBox1.Image Is Nothing) Then
Picturebox1.image. Dispose ()
PictureBox1.Image = Nothing
End if
Method 2: pass through system. Drawing. Bitmap
Dim oldImg As System.Drawing.Bitmap = PictureBox1.Image
Picturebox1.image = nothing
If IsNothing(oldImg) = False Then
Oldimg. Dispose ()
End If
Before assign image to picturebox. image, you must first dispose the original image! Otherwise, memory will continue eating!
Exam
Picturebox dispose Problem
General error system. outofmemoryexception in GDI +: insufficient memory
Example
Download example