Showing Bitmap Images in WPF via C#

19,689

Try this:

Image img = new Image();
img.Source = new BitmapImage(new Uri(@"c:\icons\A.png"));
mnuA.Icon = img;
Share:
19,689
Christopher Mann
Author by

Christopher Mann

Veteran software engineer and database architect. With more than 15 years of experience I have built solutions for a wide variety of organizations; from federal government agencies in Brazil to one of the largest health care systems in the United States. My specialties are .Net, SQL Server, Business Analytics, and user experience in general. Occasional speaker in code camps and user groups, member of the International .Net Association, president/founder of the Central California .Net User Group, Central California Give Camp, board member of the Central Valley Software Partnership in Central California, member of the International Institute of Business Analysis. I have a Bachelor's Degree in Computer Science and an MBA with emphasis in Information Systems and Entrepreneurship from California State University, Fresno. Microsoft Certified IT Professional (MCITP) in SQL Server Programming and Administration, Microsoft Certified Professional in C#, Certified Qlikview Developer and Designer.

Updated on June 28, 2022

Comments

  • Christopher Mann
    Christopher Mann almost 2 years

    What I am trying to do is so simple but I am having a hard time making it work. I saw some posts along the same lines but I still have questions.

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

    a) mnuA.Icon = new BitmapImage{UriSource = new Uri(@"c:\icons\A.png")}; Results: Instead of showing 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 showing the actual icon, I get the path of the image (file:///c:/icons/A.png)

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