Image source using project resource (WPF)

15,266

Solution 1

First, mark you image file as a "Resource" in the properties window of Visual Studio. Then you can quite easily reference the resource using the file name:

<Image x:Name="image1" Source="theimage.png" />

If you have put your image in a folder, you can use

<Image x:Name="image1" Source="/folder/theimage.png" />

Solution 2

You wanted to reference your image in XAML right?

like this

<Image Source="Resources\MyImage.png" />

and you dont need to add the image to the project resource. just add the images to your project via solution explorer

Share:
15,266
Bill Gates
Author by

Bill Gates

Updated on June 27, 2022

Comments

  • Bill Gates
    Bill Gates about 2 years

    I added a bunch of images as project resources. Now i want to use them in my WPF application using the image control. How can I assign the resource to the source of the image control?

  • Bill Gates
    Bill Gates over 14 years
    Ok the image is a resource but when I try: <Image Margin="5,5,195,35" Name="img1" Stretch="None" Source="img.jpg" /> I get the error: Value does not fall within the expected range
  • Jakob Christensen
    Jakob Christensen over 14 years
    Can you paste the entire exception message?