John Russell
John Russell

Reputation: 1165

Cannot set ImageSource using pack URI

I am trying to set an image source using the pack uri like this:

<Setter.Value>
    <ImageBrush ImageSource="pack://application:,,,/Resources/grid_bg.png"/>
</Setter.Value>

with a Resources.resx file like this:

enter image description here

with the image in it like this:

enter image description here

but the error I get is this:

kill me now

I have also attempted this:

Pack URI and path not resolving image in WPF

and this:

At design time pack uri is valid, but not at runtime?

and this:

http://csharpsimplified.wordpress.com/2009/02/16/resources-in-wpf-i-binary-resources/

while still getting the same error as above. Somebody, please, for the love of zeus, liberate me from this menial, frustrating garbage and I will return the favor with praise and points.

Upvotes: 2

Views: 4145

Answers (1)

Clemens
Clemens

Reputation: 128042

In a WPF project you usually do not add image files as resources to Resource.resx. Instead you just put them into a folder in your project and set their Build Action to Resource, as shown below.

enter image description here

It is also not necessary to write the Pack URI prefix. You can just write it like this:

<ImageBrush ImageSource="Resources/grid_bg.png"/>

Please note that if you add images to Resource.resx, Visual Studio generates a Resources class with static properties for each image. These properties are of type System.Drawing.Bitmap, which is WinForms, not WPF.

Upvotes: 5

Related Questions