[Winform] How to Use picturebox to display a picture

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.