After having the same problem as you and doing some reading, I discovered the solution - Pack URIs.
I did the following in code:
Image finalImage = new Image();
finalImage.Width = 80;
...
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri("pack://application:,,,/AssemblyName;component/Resources/logo.png");
logo.EndInit();
...
finalImage.Source = logo;
Or shorter, by using another BitmapImage constructor:
finalImage.Source = new BitmapImage(
new Uri("pack://application:,,,/AssemblyName;component/Resources/logo.png"));
The URI is broken out into parts:
The three slashes after application:
have to be replaced with commas:
Note: The authority component of a pack URI
is an embedded URI that points to a
package and must conform to RFC 2396.
Additionally, the "/" character must
be replaced with the "," character,
and reserved characters such as "%"
and "?" must be escaped. See the OPC
for details.
And of course, make sure you set the build action on your image to Resource
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…