The PictureBox component in Visual Basic is used to display images in your Visual Basic program, making it useful for programs like image browsers and previewers. There is no command directly built into the PictureBox component to save images, but a few other classes that come with Visual Basic can help you out.
- Skill level:
Other People Are Reading
Things you need
- Visual Basic 2008 or later
Drag the "PictureBox" component onto your form from the component list on the left side of the screen.
Drag the "Button" component onto your form from the component list. Drop it so that it is next to or under the PictureBox component.
Go to the "Properties" window in the bottom right of the screen and change the text property so it reads "Save."
Double-click the button you just added. This will create an event function for when the user clicks the button and automatically take you to the new function in the program editor. It will be named something similar to "Private Sub Button1_Click."
Paste the following code into the function:
Dim image As Image = PictureBox1.Image
This fetches the image data from the PictureBox and places it in an "Image" object. Though this object is invisible to the user, it contains a function, "Save," which will be very useful.
Paste the following line of code directly under the last one:
This saves the image to the file "C:\image.jpg" using the JPEG image format from the System.Drawing.Imaging.ImageFormat class.
Other supported image formats in the ImageFormat class include BMP, EMF, EXIF, GIF, GUID, ICON, JPEG, MemoryBmp, PNG, TIFF and WMF. They are referenced the same way as the JPEG image format. For example, if you want to save a PNG file, you would replace the image.Save command above with this one:
Tips and warnings
- Nothing in the image.Save command prevents you from assigning an image file the wrong file extension. For example, the following command will not generate an error:
- image.Save("C:\image.bmp", System.Drawing.Imaging.ImageFormat.Jpeg)
- Your program will proceed to save a JPEG file with a BMP file extension. This may confuse some users and image viewing applications and may cause compatibility problems for your images.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for