Displaying bitmaps in WPF via C #

What I am trying to do is so simple but hard for me to work with. I've seen several posts along the same lines, but I still have questions.

I have a MenuItem object called mnuA. All I want is to set the icon property programmatically in C #. I have tried the following

a) mnuA.Icon = new BitmapImage{UriSource = new Uri(@"c:\icons\A.png")};

Results: instead of displaying the actual icon, I get the class name ( System.Windows.Media.Imaging.BitmapImage

)

b) mnuA.Icon = new BitmapImage(new Uri(@"c:\icons\A.png"));

Results: instead of displaying the actual icon, I get the image path ( file:///c:/icons/A.png

)

What am I doing wrong? Do I really need a converter class for something simple?

thank

+1


source to share


2 answers


Try the following:



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

      

+6


source


It might be a long shot, but try something like:

Uri u = new Uri (...); mnuA.Icon = new BitmapImage (and);



This seems to happen when your icon is converted to a string.

0


source







All Articles