Philippe
Philippe

Reputation: 103

WPF Add a png image from resources with C# at runtime

I'm trying to add an image in a stack panel at runtime. My image is in the resources of the application. Here's the code that I have for the moment:

Image image = new Image();
ImageSourceConverter isc = new ImageSourceConverter();
image.Source = isc.ConvertFrom(Properties.Resources.entity16_10) as ImageSource;
image.Height = 16;
image.Width = 16;
panel.Children.Add(image);

I've got a null pointer on the line where I'm trying to use the converter, I don't know if it's the good way of doing this.

Upvotes: 2

Views: 5114

Answers (1)

GameAlchemist
GameAlchemist

Reputation: 19294

Here's how i do it :

object imguri = new Uri("/MyAssembly;Component/MyImageFolder/MyImage.png", UriKind.Relative);
BitmapImage ni = new BitmapImage(imguri);
Image img = new Image();
img.Source = ni;
return img;

Upvotes: 3

Related Questions