Loading an Image from an Embedded Resource in .NET
Disclaimer: The following information is for educational purposes only. Do not use to harass muppets.
This is one of those things that I learned to do back when I first started learning .NET, but can never remember how to do when I need it, so I'm adding it here. Hopefully this will come in handy to someone.
First, add an image to your project. You can do this by right-clicking on your project in the Solution Explorer and selecting Add->Add Existing Item, then browse for an image file using the dialog that appears. After the image has been added, click on it in the Solution Explorer and set its Build Action to Embedded Resource. Adding images to your project this way is useful for when you deploy your assembly -- it removes the need to distribute individual image files, instead embedding them into your assembly.
Next, use the following line of code to load your image from the embedded resource (examples are given in both C# and VB.NET. Please note that the below examples are each one line -- although the text appears wrapped here, you should type the code as one line; you are declaring a new Bitmap object and initializing it via a constructor):
(C#)
Bitmap bmp = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("TestBed.Galactica.bmp"));
(VB.NET)
Dim bmp as New Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("TestBed.Galactica.bmp"))
In the above examples, replace the text "TestBed.Galactica.bmp" with the assembly name and image name of the file you are using. For example, if your assembly is named "HappyAssembly" and your image is named "Smile.bmp", the text being passed to GetManifestResourceStream would be "HappyAssembly.Smile.bmp". Also, for some reason the call to GetExecutingAssembly() does not require the parentheses in VB.NET, but does (for obvious reasons) in C#, and also works with the parentheses in VB.NET.
Again, embedding your images is useful for when you deploy your app, and also prevents users from deleting or altering them. Recently, I've been playing around with non-rectangular forms (for example, an oval-shaped window), and these use bitmaps to determine their shapes. In this situation, embedding the image as a resource would ensure that the non-rectangular form is displayed as it should be. Check back here later for a posting on the ins and outs of non-rectangular forms.
This is one of those things that I learned to do back when I first started learning .NET, but can never remember how to do when I need it, so I'm adding it here. Hopefully this will come in handy to someone.
First, add an image to your project. You can do this by right-clicking on your project in the Solution Explorer and selecting Add->Add Existing Item, then browse for an image file using the dialog that appears. After the image has been added, click on it in the Solution Explorer and set its Build Action to Embedded Resource. Adding images to your project this way is useful for when you deploy your assembly -- it removes the need to distribute individual image files, instead embedding them into your assembly.
Next, use the following line of code to load your image from the embedded resource (examples are given in both C# and VB.NET. Please note that the below examples are each one line -- although the text appears wrapped here, you should type the code as one line; you are declaring a new Bitmap object and initializing it via a constructor):
(C#)
Bitmap bmp = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("TestBed.Galactica.bmp"));
(VB.NET)
Dim bmp as New Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("TestBed.Galactica.bmp"))
In the above examples, replace the text "TestBed.Galactica.bmp" with the assembly name and image name of the file you are using. For example, if your assembly is named "HappyAssembly" and your image is named "Smile.bmp", the text being passed to GetManifestResourceStream would be "HappyAssembly.Smile.bmp". Also, for some reason the call to GetExecutingAssembly() does not require the parentheses in VB.NET, but does (for obvious reasons) in C#, and also works with the parentheses in VB.NET.
Again, embedding your images is useful for when you deploy your app, and also prevents users from deleting or altering them. Recently, I've been playing around with non-rectangular forms (for example, an oval-shaped window), and these use bitmaps to determine their shapes. In this situation, embedding the image as a resource would ensure that the non-rectangular form is displayed as it should be. Check back here later for a posting on the ins and outs of non-rectangular forms.
Comments
But at
Me.PictureBox_PngSource.Image = Image.FromStream(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("Myproject.Splash.png"))
I am getting error like:-
Value of 'null' is not valid for 'stream'
:( Please help
Post a Comment