How can I store some images in a resource file at design time and fill a picture box with one at run time?
I've searched a bit on the internet but have yet to return anything I can make sense out of :-/
Thanks :)
Printable View
How can I store some images in a resource file at design time and fill a picture box with one at run time?
I've searched a bit on the internet but have yet to return anything I can make sense out of :-/
Thanks :)
Right click your project name in the solution explorer , then add new item or add existing item , then browse to your resource file (whether image , wav , icon) . When done , and the resource is there , change Build Action to embedded resource .
Thanks. Ok, I've put the JPGs in the resource file, how do I fill a picture box with one of them?
After you have added a resource image , use this :
Note , you have to change filename to your actuall project name without spaces .
Code:private void LoadResource()
{
string filename="YourProjectNameHere.ImageNameHere.jpg";
try
{
pictureBox1.Image = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream(filename) );
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
BTW , you need to use :
using System.Reflection;
at the top .
Ok, I've not done it right. Could someone please explain this from the beginning (adding the jpegs)?
Thank you :-/
Are you getting any specific error ? What part you couldn't do ?Quote:
Originally posted by TomGibbons
Ok, I've not done it right. Could someone please explain this from the beginning (adding the jpegs)?
Here's a demo . If you followed the above steps , then do the code as in the demo .
opps:D
Ok, so how do you load the pictures into the resource file in the first place? I obviously didn't do that correctly :confused:
Steps :
1-Right click your project name in the solution explorer(not the solution name) , then select from the menu Add new item (if you are going to draw the image in the IDE ) or Add existing item (to add existing image or wav file on your harddrive )
2-Then browse to your resource file (whether image , wav , icon) . You might need to change file type in the dropdown list in the Openfile dialog .
3-When done , and the resource file is there , select your image , and change Build Action in properties window to embedded resource .
4-Use the code I pasted above .
Thanks :D