In this article I will explain with an example, how to embed and read Image file from Resources in C# and VB.Net.
 
 
Adding an Embedded File
For more details on Embedded Resource, please refer Embed and read files in Windows Application (Windows Forms) in C# and VB.Net.
 
 
Form Design
The following Form consists of one Button and one PictureBox control.
Read Image from Embedded Resources in C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Reflection;
 
VB.Net
Imports System.IO
Imports System.Reflection
 
 
Reading Image file from Embedded Resources
When the Read Image File Button is clicked, an object of the Assembly class is created and assigned the reference of the executing assembly using GetExecutingAssembly method.
Then, the contents of the Image file are read using a Stream class object using the GetManifestResourceStream function of the Assembly class.
Note: In C#, you will need to access file using Namespace.FolderName.FileName, but in VB.Net there is no need to specify the name of the Folder, it can be directly accessed as Namespace.FileName.
 
Finally, the Stream class object is converted into a Bitmap class object which is ultimately assigned to the Image property of the PictureBox control.
C#
private void btnImage_Click(object sender, EventArgs e)
{
    Assembly assembly = Assembly.GetExecutingAssembly();
    Stream stream = assembly.GetManifestResourceStream("Embed_Image_File_CS.Files.Mudassar.jpg");
    picImageFile.Image = new Bitmap(stream);
}
 
VB.Net
Private Sub btnImage_Click(sender As System.Object, e As System.EventArgs) Handles btnImage.Click
    Dim assmbly As Assembly = Assembly.GetExecutingAssembly()
    Dim stream As Stream = assmbly.GetManifestResourceStream("Embed_Image_File_VB.Mudassar.jpg")
    picImageFile.Image = New Bitmap(stream)
End Sub
 
 
Screenshot
Read Image from Embedded Resources in C# and VB.Net
 
 
Downloads