Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleSave a picture into a file in VB .NET
KeywordsPathGradientBrush, VB.NET, offset, center
CategoriesGraphics, VB.NET
 
In earlier versions of Visual Basic, you use SavePicture to save an image into a file. SavePicture is missing from VB .NET so you need some other method.

This program starts by calculating the name of the file to open. It assumes it is running from the bin directory inside the main project directory that holds the target image file. It uses LastIndexOf to find "\bin" in the executable's directory, uses Substring to remove the end of the path, and adds on the file's name.

Next the program creates a new Bitmap object, passing its constructor the file's name. That loads the file into the Bitmap object. The program then calls the Bitmap object's Save method three times to save the file as bitmap, JPEG, and GIF files.

 
' Save the picture.
Private Sub btnSavePicture_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnSavePicture.Click
    ' Compose the picture's base file name.
    Dim file_name As String = Application.ExecutablePath
    file_name = file_name.Substring(0, _
        file_name.LastIndexOf("\bin")) & _
        "\test."

    ' Get a Bitmap.
    Dim bm As Bitmap = picImage.Image

    ' Save the picture as a bitmap, JPEG, and GIF.
    bm.Save(file_name & "bmp", _
        System.Drawing.Imaging.ImageFormat.Bmp)
    bm.Save(file_name & "jpg", _
        System.Drawing.Imaging.ImageFormat.Jpeg)
    bm.Save(file_name & "gif", _
        System.Drawing.Imaging.ImageFormat.Gif)

    MsgBox("Ok")
End Sub
 
This example saves the image as a bitmap, JPEG, and GIF but the Save method can save images in many other formats. The allowed formats are Bmp, Emf, Exif, Gif, Icon, Jpeg, MemoryBitmap, Png, Tiff, and Wmf.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated