tags:

views:

48

answers:

5

I have image in project added as resource, if I try to add it to my WPF project it always end up with

Error 1 The file images\background.png is not part of the project or its 'Build Action' property is not set to 'Resource'. C:\Users\Martinek\Documents\My\Learning.Dot.Net\WPF.8\WPF.8\Window1.xaml 21 47 WPF.8

I also tried to reference full path "file:///" etc. and same results even when image is added as resource

See also XAML code with results

<ImageBrush AlignmentX="Left" ImageSource="images/background.png" Stretch="Fill" />

Attaching image: http://i.imgur.com/bSjwi.png

+1  A: 

Try the pack syntax:

<ImageBrush ImageSource="pack://application:,,,/Images/background.png" />
Wonko the Sane
do not work. Surprisingly when I place background.png to root directory it works without any problems at all. This only happens when image is in directory
markoniuss
You may have to do the extended version of the command, adding the assembly name and component.
Wonko the Sane
+1  A: 
<ImageBrush AlignmentX="Left" ImageSource="pack://application:,,,/[ASSEMBLY_NAME];component/Images/background.png" Stretch="Fill" />

[ASSEMBLY_NAME] is the name of the dll or executable that contains the resource (without an extension).

Khyad Halda
+1  A: 

I noticed that you have the Copy to Output Directory option set to "Do Not Copy."

Try changing it to either "Copy Always" or "Copy if newer" and see if that helps.

Update

I just wrote a quick sample app to try to figure this out. It seems to work properly, so I'll post my code here hoping that it'll help.

<Grid>
  <Grid.Background>
    <ImageBrush x:Name="brush" AlignmentX="Left" ImageSource="images/have_the_dumb.jpg" Stretch="Fill" />
  </Grid.Background>
</Grid>

In the code-behind, I added some tracing code to see where the ImageBrush thinks its ImageSource is.

    public ImageDisplay()
    {
      Trace.Listeners.Add(new TextWriterTraceListener(@"c:\happyface.trace.log"));
      Trace.AutoFlush = true;

      InitializeComponent();

      Trace.WriteLine(String.Format("Image thinks it's in {0}", brush.ImageSource.ToString()));
    }

When running that in Debug mode, you should see a line written to the Output window (as well as to the *.trace.log file) with the URI where it thinks the image is located.

I ran it with the image in an images folder off the root of my solution. I tried it with the image set as "Content" (with "Copy if newer") as well as "Resource" (with do not copy), and it ran successfully both times.

Hope this points you in the right direction.

Eric
The Build Action is Resource, not Content
Wonko the Sane
Thanks, what I have seen is a difference between how file is added to project. If I drag'n'drop file from Total Commander to project window it works perfectly. If I added a file through Resources Window (project properties->Resources->Images->Add) it did not work even when in result it look's same.
markoniuss
A: 

I have two ideas - use a backslash instead of a forward slash. (It shouldn't matter - but that's what works here...)

The other idea is that your project path looks messed up - try copying the solution to the root of your harddrive. You might have hit some weird problem with the path containing too many periods/slashes or whatever.

Goblin
I tried it, there is no difference betweem back and forward slash
markoniuss
A: 

pls read this reference. This is having different solutions

http://stackoverflow.com/questions/347614/wpf-image-resources

Kishore Kumar