none
Loading jpg resource from code behind

    Question

  • I have an icon that is located on the disk in an Images folder and I have added it to my project.  How do I load this image from C# code?  It works fine from XAML.  My code is this:

    Uri uri = new Uri(@"\Images\Mail.jpg", UriKind.RelativeOrAbsolute);

    BitmapImage bmi = new BitmapImage(uri);

    I've played around with the path information in the URI to no avail.  When I run the app, no image is displayed.

    Thanks.

    Wednesday, October 01, 2008 9:23 PM

Answers

  • Hi Peter,

    Do you want the image embedded as a resource or as an actual referenced file on disk?

    If you want it as an resource, given the following file structure, do the following...

    -- App.xaml
    -- Window1.xaml
    -- Window1.xaml.cs
    -- Images
                    -- Mail.jpg

    1. In Solution Explorer, right click on the file Mail.jpg, select properties, make sure the build action is "Resource".
    2. In your *.cs file write the following code to access it:

    BitmapImage bmi = new BitmapImage(new Uri("/Images/Mail.jpg", UriKind.Relative));

    That should be it.

    If you want it as a file on disk (ie. not embedded in your executable) do the following...

    1. In Solution Explorer, right click on the file Mail.jpg, select properties, make sure the value for the "Copy to Output Directory" is "Copy if newer", make sure the value for the "Build Action" property is "None".
    2. In your *.cs file write the following code to access the image:

    BitmapImage bmi = new BitmapImage(new Uri("pack://siteoforigin:,,,/Images/mail.jpg", UriKind.Absolute));

    That should do it.

    The siteoforigin keyword in the URI simply refers to the location of the executing assembly. If you want to know more about the "pack syntax" used for the second example simply type those two words into google and you'll find a ton of good resources.

    Hope that helped,

    Josh.
    • Marked as answer by Peter-H Thursday, October 02, 2008 2:55 PM
    Wednesday, October 01, 2008 10:58 PM
  • By the way, if you want to embed it as a resource in your application I would suggest using the following code instead:

    BitmapImage bmi = new BitmapImage(); 
    bmi.BeginInit(); 
    bmi.StreamSource = Application.GetResourceStream(new Uri("/Images/mail.jpg", UriKind.Relative)).Stream; 
    bmi.EndInit(); 

    The reason I find this better is because if you reference the resource by URI using either the BimapImage constructor or the UriSource property of the BitmapImage object, if someone removes/renames the resource file your application will still compile, run, and work until it renders the BitmapImage object - at which point it will crash with an internal WPF error message. If however you reference your embedded resource using the above code, if the resource file is not available when it runs the "bmi.StreamSource = Applic..." line it will crash straight away at the point of failure.

    If you have a reasonably sized application with several people working on it this method doesn't take long to pay off when people start renaming resource files without updating all of the references.

    - Josh
    • Marked as answer by Peter-H Thursday, October 02, 2008 2:55 PM
    Wednesday, October 01, 2008 11:09 PM

All replies

  • Hi Peter,

    Do you want the image embedded as a resource or as an actual referenced file on disk?

    If you want it as an resource, given the following file structure, do the following...

    -- App.xaml
    -- Window1.xaml
    -- Window1.xaml.cs
    -- Images
                    -- Mail.jpg

    1. In Solution Explorer, right click on the file Mail.jpg, select properties, make sure the build action is "Resource".
    2. In your *.cs file write the following code to access it:

    BitmapImage bmi = new BitmapImage(new Uri("/Images/Mail.jpg", UriKind.Relative));

    That should be it.

    If you want it as a file on disk (ie. not embedded in your executable) do the following...

    1. In Solution Explorer, right click on the file Mail.jpg, select properties, make sure the value for the "Copy to Output Directory" is "Copy if newer", make sure the value for the "Build Action" property is "None".
    2. In your *.cs file write the following code to access the image:

    BitmapImage bmi = new BitmapImage(new Uri("pack://siteoforigin:,,,/Images/mail.jpg", UriKind.Absolute));

    That should do it.

    The siteoforigin keyword in the URI simply refers to the location of the executing assembly. If you want to know more about the "pack syntax" used for the second example simply type those two words into google and you'll find a ton of good resources.

    Hope that helped,

    Josh.
    • Marked as answer by Peter-H Thursday, October 02, 2008 2:55 PM
    Wednesday, October 01, 2008 10:58 PM
  • By the way, if you want to embed it as a resource in your application I would suggest using the following code instead:

    BitmapImage bmi = new BitmapImage(); 
    bmi.BeginInit(); 
    bmi.StreamSource = Application.GetResourceStream(new Uri("/Images/mail.jpg", UriKind.Relative)).Stream; 
    bmi.EndInit(); 

    The reason I find this better is because if you reference the resource by URI using either the BimapImage constructor or the UriSource property of the BitmapImage object, if someone removes/renames the resource file your application will still compile, run, and work until it renders the BitmapImage object - at which point it will crash with an internal WPF error message. If however you reference your embedded resource using the above code, if the resource file is not available when it runs the "bmi.StreamSource = Applic..." line it will crash straight away at the point of failure.

    If you have a reasonably sized application with several people working on it this method doesn't take long to pay off when people start renaming resource files without updating all of the references.

    - Josh
    • Marked as answer by Peter-H Thursday, October 02, 2008 2:55 PM
    Wednesday, October 01, 2008 11:09 PM
  • Hi Josh,

    Thank you for your excellent answer.  I have it working now.  Much appreciated.

    Peter
    Thursday, October 02, 2008 2:55 PM