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
 
 
 
 
 
TitleEmbed bitmap, text, and other resource files in a compiled application and use them in VB .NET
DescriptionThis example shows how to embed bitmap and text files in a compiled Visual Basic .NET application. At run time, the program extracts these files and displays their contents.
Keywordsembed, embedded resource, resource, bitmap
CategoriesVB.NET
 
To embed the files, open the Project menu and select Add Existing Item. Select the files to add them to the Solution Explorer. In the file selection dialog, set the "Files of type" dropdown to All Files so you can see the files.

Click on the files in Solution Explorer and open the Properties window. Select the file's Build Action property, click the dropdown arrow on the right, and select Embedded Resource. Now when you compile the program, the files are embedded as resources in the application.

To load the files at run time, the program gets its assembly. Resource files stored in the application in this manner have names of the form assembly_name.file_name so the program uses the assmebly's GetName().Name.ToString() method to get the assembly's name.

To get an embedded file, the program uses the assembly's GetManifestResourceStream method to get a stream attached to the file's data. To read a bitmap file, the program makes a Bitmap passing the stream to the Bitmap's constructor.

To read a text file, the program makes a StreamReader, passing the stream to its constructor.

 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Get our assembly.
    Dim executing_assembly As System.Reflection.Assembly = _
        Me.GetType.Assembly.GetEntryAssembly()

    ' Get our namespace.
    Dim my_namespace As String = _
        executing_assembly.GetName().Name.ToString()

    ' Load three pictures.
    Dim picture_stream As Stream
    Dim bm As Bitmap

    picture_stream = _
        executing_assembly.GetManifestResourceStream(my_namespace _
        + ".picture1.bmp")
    If Not (picture_stream Is Nothing) Then
        bm = New Bitmap(picture_stream)
        PictureBox1.Image = bm
        picture_stream.Close()
    End If

    picture_stream = _
        executing_assembly.GetManifestResourceStream(my_namespace _
        + ".picture2.bmp")
    If Not (picture_stream Is Nothing) Then
        bm = New Bitmap(picture_stream)
        PictureBox2.Image = bm
        picture_stream.Close()
    End If

    picture_stream = _
        executing_assembly.GetManifestResourceStream(my_namespace _
        + ".picture3.bmp")
    If Not (picture_stream Is Nothing) Then
        bm = New Bitmap(picture_stream)
        PictureBox3.Image = bm
        picture_stream.Close()
    End If

    ' Load a text file.
    Dim text_stream As Stream = _
        executing_assembly.GetManifestResourceStream(my_namespace _
        + ".text1.txt")
    If Not (text_stream Is Nothing) Then
        Dim stream_reader As New StreamReader(text_stream)
        Label1.Text = stream_reader.ReadToEnd()
        stream_reader.Close()
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated