14

Can anyone show me how can I set source to image from Resource in XAML. Currently I have

 <Image x:Name="img" Panel.ZIndex="1" Source="D:\Img100.jpg"  Stretch="Uniform"/>

I want Image Source to be come from Resources.

Thanks.

1 Answer 1

33

First you will have to include the image in your project with BuildAction Resource

Then you can use it directly in your Image

 <Image Source="Img100.jpg"/>

Or make a reusable resource of the image

App.xaml (or other resource dictionary)

<Application.Resources>
    <BitmapImage x:Key="myImage" UriSource="Img100.jpg" />
</Application.Resources>

Element:

<Image Source="{StaticResource myImage}" />
Sign up to request clarification or add additional context in comments.

3 Comments

A small modification: <BitmapImage x:Key="myImage">pack://application:,,,/Resources/Img100.jpg</BitmapImage>
@Sonhja why is the pack syntax needed?
@DaveInCaz it's a pack resource for a XAML resource. You can see details here: learn.microsoft.com/en-us/dotnet/framework/wpf/app-development/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.