How to use image from resources?

Viewed 1518

I would like to use path to image from program resources instead of using full path from windows.

tlo.Fill = new ImageBrush
{
  ImageSource = new BitmapImage(new Uri(@"D:\Willie\Documents\ColorTester\ColorTester\Resources\1.jpg", UriKind.Absolute))
};

I want to use path like this:

ImageSource = new BitmapImage(new Uri("ColorTester.Resources.1.jpg"));

Can anyone help me with this, because when I try to use it, visual studio throws and exception "System.UriFormatException"?

1 Answers

I don't know the correct path to your image, if you have a folder named Resources under your project and images are in there, you can address them like below:

ImageSource = new BitmapImage(new Uri("/Resources/1.jpg", UriKind.Relative));

If the image is inside a UserControl and you want to use it in another project, then address it like:

ImageSource = new BitmapImage(new Uri("pack://application:,,,/{YourAssemblyWhereResourceIsLocated};component/Resources/1.jpg"));

Useful links:

Related