Sam
Sam

Reputation: 29009

wpf how to use jpg from resource in xaml-image?

In my c#/WPF project, I added a jpg to the resources to embed into the exe file.

Now I want to use this jpg in an image tag, something like

<xmlns:prop="clr-namespace:MyProgram.Properties"
<Image Source="{Binding Source={StaticResource prop:LogoJpg}}"

My problem: it does not work. I got no idea, how to use the image. I could use files from the hdd, but I need the image to be embedded in the exe file.

Upvotes: 1

Views: 7677

Answers (1)

Wonko the Sane
Wonko the Sane

Reputation: 10813

First, add the image to your project, with a Build Action of "Resource" (not EmbeddedResource). For instance, I've added an image called "logo.jpg" to a folder called Images in my main project.

Then, in XAML, you use just use that resource as follows:

<Image Source="Images\logo.jpg" />

You can also use the pack syntax for the source:

<Image Source="pack://siteoforigin:,,,/Images/logo.jpg"  />

Hope this helps.

Upvotes: 4

Related Questions