2

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.

Community
  • 1
  • 1
John Russell
  • 1,115
  • 1
  • 15
  • 30
  • does grid_pg have the correct access modifier? – Sayse Aug 22 '13 at 06:32
  • internal? public? no code generation? all of them fail. :/ – John Russell Aug 22 '13 at 06:33
  • I have also tried to have them linked at compile time and embedded into the .resx file - no luck there either. – John Russell Aug 22 '13 at 06:34
  • Is your Resource file .png inside a "Resources" folder in the current project and I think you should also include your assembly name in the pack uri? – S2S2 Aug 22 '13 at 06:35
  • Yes sir/maam -> http://i.imgur.com/WXhFE7s.png – John Russell Aug 22 '13 at 06:38
  • I am able to access it programatically as well using [project_name].Resources.grid_bg – John Russell Aug 22 '13 at 06:39
  • If your resource file is in local project, you should be able to use: ImageSource="../../Resources/grid_bg.png" instead of a pack uri syntax.. – S2S2 Aug 22 '13 at 06:52
  • Can you pls also check that Build Action for file /Resources/grid_bg.png is "Resource"... for using pack uri syntax.. – S2S2 Aug 22 '13 at 07:12
  • Is is currently set to Resource. I should note that I have currently defined a ResourceDictionary in Application.Resources in App.xaml to define a styling template for some UI elements. Does this have anything to do with it? I just find it so puzzling that I can access it from the .cs by not from the App.xaml ... – John Russell Aug 22 '13 at 07:22
  • I should also note that when I feed it the full path from disk, it works correctly as well. (ex: ImageSource="C:\path\to\button_bg.png" ) – John Russell Aug 22 '13 at 07:24

1 Answers1

4

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.

Clemens
  • 123,504
  • 12
  • 155
  • 268