通过C#在WPF中显示位图图像

Gus*_*nti 1 wpf icons bitmapimage

我想要做的是如此简单,但我很难让它工作.我在同一行看到了一些帖子,但我仍然有疑问.

我有一个名为mnuA的MenuItem对象.我想要的只是在C#中以编程方式设置icon属性.我尝试了以下内容

a)mnuA.Icon = new BitmapImage{UriSource = new Uri(@"c:\icons\A.png")}; 结果:我没有显示实际的图标,而是获得了类名(System.Windows.Media.Imaging.BitmapImage)

b)mnuA.Icon = new BitmapImage(new Uri(@"c:\icons\A.png")); 结果:我没有显示实际图标,而是获得图像的路径(file:///c:/icons/A.png)

我究竟做错了什么?对于像这样简单的事情,我真的需要一个转换器类吗?

w4g*_*n3r 6

试试这个:

Image img = new Image();
img.Source = new BitmapImage(new Uri(@"c:\icons\A.png"));
mnuA.Icon = img;
Run Code Online (Sandbox Code Playgroud)