locked
How to load embedded picture into image control from code RRS feed

  • Question

  • I would like to load a picture I embedded into my project, into an image control, using C# code.

    1. I added the file "Password_Icon.jpg" to my project, via the Project | Add Existing Item command in Visual Studio.
    2. I set the Build Action property of the picture file to "Embedded Resource."
    3. Next, I added the following code to my app.
            private void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
                Stream myStream = myAssembly.GetManifestResourceStream("DC_Password_Saver.Password_Icon.jpg");
                System.Windows.Media.Imaging.BitmapImage image = new System.Windows.Media.Imaging.BitmapImage();
    
                image.StreamSource = myStream;
                MyImageControl_image.Source = image;
            }

    Note: DC_Password_Saver, is the Namespace for the main window for my app. This is why it precedes the name of my app, in the 4th line of the code.

    The code above executes, but nothing happen. Can someone suggest a solution to my problem? Thanks in advance for your help.

    • Edited by PDoug Monday, February 16, 2015 6:15 PM
    Monday, February 16, 2015 6:14 PM

Answers

  • My app is called wpf11.

            public MainWindow()
            {
                InitializeComponent();
    
                var bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = Assembly.GetExecutingAssembly().GetManifestResourceStream("wpf11.DSC00068.JPG");
                bi.EndInit();
                img.Source = bi;

    Substitute the name of your application ( or the namespace your image is in ) for wpf11.

    Also the name of your jpg and control, obviously ;^)


    Hope that helps.
    Recent Technet articles: Property List Editing ;   Dynamic XAML

    • Edited by Andy ONeill Monday, February 16, 2015 6:59 PM
    • Marked as answer by PDoug Monday, February 16, 2015 8:02 PM
    Monday, February 16, 2015 6:58 PM

All replies

  • My app is called wpf11.

            public MainWindow()
            {
                InitializeComponent();
    
                var bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = Assembly.GetExecutingAssembly().GetManifestResourceStream("wpf11.DSC00068.JPG");
                bi.EndInit();
                img.Source = bi;

    Substitute the name of your application ( or the namespace your image is in ) for wpf11.

    Also the name of your jpg and control, obviously ;^)


    Hope that helps.
    Recent Technet articles: Property List Editing ;   Dynamic XAML

    • Edited by Andy ONeill Monday, February 16, 2015 6:59 PM
    • Marked as answer by PDoug Monday, February 16, 2015 8:02 PM
    Monday, February 16, 2015 6:58 PM
  • You're the man! Thanks a lot!
    Monday, February 16, 2015 8:04 PM
  • You're welcome.

    Hope that helps.
    Recent Technet articles: Property List Editing ;   Dynamic XAML

    Monday, February 16, 2015 8:11 PM