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
 
 
 
 
 
 
TitleLoad a picture from a file in VB .NET
Keywordsload picture, loadpicture, VB.NET
CategoriesGraphics, VB.NET
 
In earlier versions of Visual Basic, you use LoadPicture to load an image from a file. LoadPicture 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 sets a PictureBox's Image property to the Bitmap to display the result. It also sets the control's SizeMode property to AutoSize so the whole image is visible.

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

    ' Load the picture into a Bitmap.
    Dim bm As New Bitmap(file_name)

    ' Display the results.
    picImage.Image = bm
    picImage.SizeMode = PictureBoxSizeMode.AutoSize
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated