开发者

WPF, resource image not correctly located

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.stack.imgur.com/bSjwi.png


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.


Try the pack syntax:

<ImageBrush ImageSource="pack://application:,,,/Images/background.png" />


<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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜