WPF ImageSource

A little How-to put an image from embedded resources into your WPF application. I have found it very useful especially when working with ListView and creating value converters for it.

My solution for this examples looks like that:

ImageSourceExampleSolution

 

1) ImageSource in XAML:

<Image Name="image1" 
Source="/ImageSourceExample;component/Assets/flower.jpg" />

Where ImageSourceExample is the assembly name.

2) ImageSource in c#

ImageSourceConverter imgConv = new ImageSourceConverter();
string path = "pack://application:,,/Assets/flower.jpg"
ImageSource imageSource = (ImageSource) imgConv.ConvertFromString(path);
image1.Source = imageSource;

To read more about pack URIs in WPF look here.

Monika

No Comments