Click here to Skip to main content
11,490,003 members (70,032 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
I have a C# winForm project that uses a ContextMenuStrip. I dynamically add ToolStripMenuItems to the ContextMenuStrip based on use interaction. When I add a new ToolStripMenuItem I set it's Text property and Image property. I don't know how to the set the Image property without getting the image from the location where it's at. How do I add the imagine to my project? Here's an example of what my code is doing

ContextMenuStrip cxtMnuStrp = new ContextMenuStrip;
private void Button_Click(object sender, EventArgs e)
   // some filtering and logic
   // to determine weather to 
   // create and add a ToolStripMenuItem
   // blah, blah, blah...
   ToolStripMenuItem item = new ToolStripMenuItem("uniqueName");
   item.Image = Image.FromFile(@"C:\MyFolder\MyIcon.ico");
   if (cxtMnuStrp.Items.ContainsKey(item) == false)

With "item.Image = Image.FromFile(@"C:\MyFolder\MyIcon.ico")" When I distribute my each machine would have to have the "C:\MyFoler" directory and also have the "MyIcon.ico" on their computer in the "C:\MyFoler" directory.

Plus it doesn't seem right that I have hit the hard drive each time I want to add an icon to my ToolStripMenuItem
Posted 23-Apr-13 16:38pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Add the image as an embedded resource in your project, then you can use Image.FromStream and Assembly.GetManifestResourceStream to load your image. You can also add a settings file and add the images to the settings file then just use the properties of the settings file to get the image.
Donald Allen at 23-Apr-13 22:44pm
can you please give an example with code?
Ron Beyer at 23-Apr-13 22:47pm
This has lots of examples:
Donald Allen at 24-Apr-13 12:38pm
Thanks that worked!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 595
1 Andy Lanng 233
2 OriginalGriff 213
3 Maciej Los 170
4 _duDE_ 141
0 Sergey Alexandrovich Kryukov 9,608
1 OriginalGriff 8,475
2 Sascha Lefèvre 3,419
3 Maciej Los 3,106
4 Richard Deeming 2,370

Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100