Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How can I set the image property at run time?
Message
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00976594
Message ID:
00976742
Views:
9
Neil,
Thank you for the reply. I still have not got my code to work the way I want it to work.

Please review and let me know where you think I am going wrong.

1. I added two images Img0.jpg and Img1.jpg to my solution
2. I changed the Build Action in the file properties to Embedded Resource.
3. In the constructor (after the InitializeComponent()) I have the following code:
oBitmaps = new Bitmap[2]; // Should I maybe use an array of images instead of bitmaps?

//System.Resources.ResourceManager oResMan = new System.Resources.ResourceManager("MyScreenSaver.frmMain",System.Reflection.Assembly.GetExecutingAssembly());
System.Resources.ResourceManager oResMan = new System.Resources.ResourceManager(typeof(frmMain));

oBitmaps[0] = (Bitmap) oResMan.GetObject("Img0.jpg");  // oResMan.GetObject("NetMasterLogo") returns null
oBitmaps[1] = new Bitmap("c:\\it\\Img1.jpg");  // this works and loads the image into the bitmap array
4. At another point in my application I set the image property of the picturebox using the code:
this.pictureBox1.Image = this.oBitmaps[iImage];
I don't really understand the code you have in button1_click. I thought the purpose of embedding an image into the exe through the build action of the file properties was that I didn't have to ship the image files but that they would be embedded in the exe.

I hope you can help me.

Thanks,
Einar

>Einar,
>
>If you have embedded resources you can use the ResourceManager class in the System.Resources namespace. There are lots of examples available but the following code shows you how to create a custom resource file and read from it (an image and a string).
>
>private void button1_Click(object sender, System.EventArgs e)
>{
>   ResourceWriter imageResource = new ResourceWriter("quotes.resources");
>
>   Image testImage = Image.FromFile(@"c:\test.bmp");
>
>   imageResource.AddResource("msg1", "This is a string");
>   imageResource.AddResource("neil", testImage);
>   imageResource.Close();
>}
>
>private void button2_Click(object sender, System.EventArgs e)
>{
>   ResourceManager imageResManager = new ResourceManager("WindowsApplication1.quotes", Assembly.GetExecutingAssembly());
>	
>   pictureBox1.Image = (Image)imageResManager.GetObject("neil");
>   MessageBox.Show((string)imageResManager.GetObject("msg1"));
>}
>
>Three things to note.
>
>1] The button1_Click event generates the resource file, you need to add this to your project and rebuild it with the Build Action as Embedded Resource.
>
>2] You prefix the name of the resource with the Namespace otherwise you will get exceptions.
>
>3] If you are using cultures then you will have to supply culture specific resources and use the overloaded GetObject passing in the correct culture.
>
>Regards
>Neil
Semper ubi sub ubi.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform