How to make Uri reference to a Resource image embedded in the assembly itself? [duplicate]

StackOverflow https://stackoverflow.com/questions/22664534

  •  21-06-2023
  •  | 
  •  

Question

In my assembly project I have a .png image which it's Build Action is set to Resource.

Now I want to make the following:

_myIcon = new BitmapImage(new Uri(**path to the image embedded on the assembly**));

I have looked into the examples in http://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx but as far as I tried gives me "Invalid URI: The format of the URI could not be determined.". I don't think this should be a complicated matter, am I taking the wrong approach?

Was it helpful?

Solution

The other answer is valid but doesn't produce a BitmapSource.

The correct answer, given that I have a folder called Images and inside an Image with the Build Action set to Resource, is to use a Resource File Pack URI:

_myIcon = new BitmapImage(new Uri("pack://application:,,,/Images/MyImage.png");

OTHER TIPS

For me it worked with something like this

String filename = this.GetType().Namespace + ".Resources.myimg.bmp";
//or String.Join if you like
_myIcon = Bitmap.FromStream(Assembly.GetExecutingAssembly()
                                .GetManifestResourceStream(filename))

So FromStream + GetManifestResourceStream should be the magic words

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top