Problem with relative path to image in XAML?

Posted by Giri on Stack Overflow See other posts from Stack Overflow or by Giri
Published on 2010-04-19T05:08:38Z Indexed on 2010/04/19 5:13 UTC
Read the original article Hit count: 348

Filed under:
|
|
|
|

I am trying to reference a PNG file in my applications working directory through XAML with the following:

<Image Name="contactImage">
    <Image.Source>
        <BitmapImage UriSource="/Images/contact.png">
    </Image.Source>
</Image>

Now in my code-behind I try to get the height of the image with contactImage.Source.Height

This fails with System.IOException - cannot locate resource 'images/contact.png'. If I use something like

PngBitmapDecoder p = new PngBitmapDecoder(new Uri("./Images/contact.png"), UriKind.Relative, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

Everything is happy. How can I reference an image in xaml to a path relative to the working deirectory of the app. BTW- this is being run on a remote machine (if that makes a difference). I have tried "./Images/contact.png" and ".\Images\contact.png" and several other combinations of back/forward slashes and dots. Here is the primary difference-

Any time the file is referenced in XAML, it shows up as pack://aplication:,,, blah blah blah when I use the PngBitmapDecoder, it shows up correctly as "./Images/contact.png".

How do I reference the image file in XAML and get it show a source as "./Images/contact.png" instead of a pack://application,,,blah blah blah?

© Stack Overflow or respective owner

Related posts about reference

Related posts about image