I have an images folder with a png in it. I would like to set a MenuItem's icon to that png. How do I write this in procedural code?
Asked
Active
Viewed 6.0k times
8 Answers
66
menutItem.Icon = new System.Windows.Controls.Image
{
Source = new BitmapImage(new Uri("images/sample.png", UriKind.Relative))
};

Isak Savo
- 34,957
- 11
- 60
- 92

Timothy Fries
- 2,161
- 19
- 8
-
This helped us tremendously. We had to first create an Image, then assign the source of that Image to the BitMapImage, then set menuItem.Icon = Image object, but it worked. – ScottG May 19 '09 at 21:14
-
3I edited the answer so that new visitors to this question gets working code in case they miss this comment thread – Isak Savo Dec 02 '11 at 07:16
23
<MenuItem>
<MenuItem.Icon>
<Image>
<Image.Source>
<BitmapImage UriSource="/your_assembly;component/your_path_here/Image.png" />
</Image.Source>
</Image>
</MenuItem.Icon>
</MenuItem>
Just make sure your image in also included in the project file and marked as resource, and you are good to go :)
-
3Original question was for procedural code. Also, I believe in XAML you could write `
` inside the ` ` – Neil Sep 14 '10 at 10:51 -
1+1 for showing the right way... XAML way... to do it. @Neil, +1 to you and I edited to your suggestion, as that is the correct way to do it – Serj Sagan Jan 21 '13 at 08:22
-
4Just because it is done in XAML does not necessarily make it the right way. – Stephen Drew Jan 08 '14 at 15:19
18
Arcturus's answer is good because it means you have the image file in your project rather than an independent folder.
So, in code that becomes...
menutItem.Icon = new Image
{
Source = new BitmapImage(new Uri("pack://application:,,,/your_assembly;component/yourpath/Image.png"))
}

Joan Venge
- 315,713
- 212
- 479
- 689

IanR
- 4,703
- 3
- 29
- 27
3
This is a bit shorter :D
<MenuItem Header="Example">
<MenuItem.Icon>
<Image Source="pack://siteoforigin:,,,/Resources/Example.png"/>
</MenuItem.Icon>
</MenuItem>

Basti Endres
- 39
- 1
1
This is what worked for me
<MenuItem Header="delete ctrl-d" Click="cmiDelete_Click">
<MenuItem.Icon>
<Image>
<Image.Source>
<ImageSource>Resources/Images/delete.png</ImageSource>
</Image.Source>
</Image>
</MenuItem.Icon>
</MenuItem>

Gary93730
- 431
- 4
- 13
1
This is how I used it (this way it dont need to be built into the assembly):
MenuItem item = new MenuItem();
string imagePath = "D:\\Images\\Icon.png");
Image icon = new Image();
icon.Source= new BitmapImage(new Uri(imagePath, UriKind.Absolute));
item.Icon = icon;

awe
- 21,938
- 6
- 78
- 91
0
For those of you using vb.net, to do this you need to use this:
menuItem.Icon = New Image() With {.Source = New BitmapImage(New Uri("pack://application:,,,/your_assembly;component/yourpath/Image.png"))}

Python Kid
- 313
- 2
- 7
- 19
-5
You can also use your Visual Studio to insert a icon. This is the easiest way
- Right click at you project in the solution explorer
- chose Properties
- Make sure you're in the application page.
- @ recources you see: Icon and Manifest
- @ Icon: Click browse and pick your icon.
Problem solved.

Mars
- 1
-
2This works for WinForms. The question was how to set it manually from code. – Alexandru Dicu Oct 05 '12 at 20:21