.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > relative filepath for BitmapImage UriSource in Feb CTP
Ask a questionAsk a question
 

Answerrelative filepath for BitmapImage UriSource in Feb CTP

  • Tuesday, March 14, 2006 1:09 PMbitbonk Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    How do do I specify a relative filepath for an Image using feb CTP 06 ?

    I copied myimage.jpg into the same directory as the exe (bin/Debug and bin/Release)

    and I tried to use the pack syntax as the docs suggest:

    <Image>
    <Image.Source>
    <BitmapImage UriSource="pack://application:,,/myimage.jpg" />
    </Image.Source>
    </Image>

    also I tried

    UriSource="myimage.jpg"

    In both cases I get a System.IO.Exception, saying: "cannot locate ressource myimage.jpg"

    what am I doing wrong?

     

     

Answers

  • Tuesday, March 14, 2006 5:17 PMAshish Shetty - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You need to declare the image as a Resource or a Content file in your project. The former embeds the image in your assembly and the latter retains it loosely alongside the application. If you declare it as Content, make sure to set the CopyToOutputDirectory meta-data to the appropriate value (Always or PreserveNewest). Then in markup or code, you can use a simple relative Uri like "myimage.jpg". This relative Uri is equivalent to "pack://application:,,,/myimage.jpg" but is far more convenient.

    WPF apps cannot load arbitrary resources from disk.

    The site of origin syntax can be used to reference and load files that are not known/declared at compile time.

    For more information on when to use what build action, see http://nerddawg.blogspot.com/2005/11/resources-in-windows-presentation.html

All Replies

  • Tuesday, March 14, 2006 1:55 PMKimme Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hy bitbonk,

    try to use "pack://siteoforigin:,,,/myimage.jpg"

    hope this helps,
    kimme
  • Tuesday, March 14, 2006 5:17 PMAshish Shetty - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You need to declare the image as a Resource or a Content file in your project. The former embeds the image in your assembly and the latter retains it loosely alongside the application. If you declare it as Content, make sure to set the CopyToOutputDirectory meta-data to the appropriate value (Always or PreserveNewest). Then in markup or code, you can use a simple relative Uri like "myimage.jpg". This relative Uri is equivalent to "pack://application:,,,/myimage.jpg" but is far more convenient.

    WPF apps cannot load arbitrary resources from disk.

    The site of origin syntax can be used to reference and load files that are not known/declared at compile time.

    For more information on when to use what build action, see http://nerddawg.blogspot.com/2005/11/resources-in-windows-presentation.html

  • Wednesday, March 15, 2006 8:52 AMKimme Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    This doesn't seem to work when I'm trying to set my cursor. new Cursor("cursor1.cur") specifically asks for an absolute path. Is there something I can do about that?
  • Wednesday, March 15, 2006 9:51 AMXASD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It's not working-not "Resource" nor "Embedded resource" and "pack:" syntax dosen't work ever,it's only working with "Content" approach-

    Error at element 'ImageBrush' in markup file 'window1.xaml' : 'test.jpg' string is not a valid value for 'ImageSource' property of type 'ImageSource'..

    markup:

    <ImageBrush ImageSource="test.jpg"/>


    Regards.
  • Thursday, March 16, 2006 1:26 AMAshish Shetty - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I declared one image file foo.jpg as Resource and another bar.jpg as Content. Then in markup I referenced them like so:

    <Image Source="foo.jpg"/>  and  <Image Source="bar.jpg"/>

    Works fine on Feb CTP. I also tried:

    <Image Source="pack://application:,,,/foo.jpg"/>  and  <Image Source="pack://application:,,,/bar.jpg"/>

    Again, no issues.

    Then I dropped an image file quz.jpg into the bin\Release folder from where I launch the application (i.e. the application's site of origin). This image is not declared in my project. I then added this to the markup file:

    <Image Source="pack://siteoforigin:,,,/quz.jpg"/>

    No issues there as well. Let me know if that works for you.

  • Thursday, March 16, 2006 1:37 AMNick Kramer - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     Kimme wrote:
    This doesn't seem to work when I'm trying to set my cursor. new Cursor("cursor1.cur") specifically asks for an absolute path. Is there something I can do about that?

    Thanks for the bug report, we're working on making these various URI consumers more consistent, but at present Cursor really does require an absolute path.

  • Thursday, March 16, 2006 11:50 AMXASD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It seems there's inconsistency in API,I thought image source will be handled same way but <Image/> and <ImageBrush/> treat image source different.
    Try <ImageBrush/> youself.

    Thanks.