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
The following Form consists of one Button and one PictureBox control.
You will need to import the following namespaces.
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.
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);
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)