locked
Image Pathing RRS feed

  • Question

  • I can't figure out how to path to an image that resides with in the project. I don't want to hard code the image path to the project then the image and I know there is a way to do this.

    I can't figure out what I need to do to path the image so that it know that the image resides in the project in the images directory.

    Currently this:
    Uri myUri = new Uri("/images/" + GetBackGroundImage() + ".jpg", UriKind.Relative);

    Will get me this
    C:\images\image.jpg

    So what do i need to do in order to get the Uri to be something like ????? "c:\mydocuments\projects\sampleproject\images\background01.jpg"

    Wednesday, October 29, 2008 7:46 PM

Answers

  • Acutally This is what I went with:
    pack://application:,,/images/Background01.jpg 

    xaml:
    <ImageBrush x:Name="Main_Background" 
            ImageSource="pack://application:,,/images/Background01.jpg"/> 

    Code Behind:
    Uri myUri = new Uri("pack://application:,,/images/" + GetBackGroundImage() + ".jpg");  
    this.Main_Background.ImageSource = new BitmapImage(myUri); 
    • Marked as answer by samzon Thursday, October 30, 2008 4:19 PM
    • Edited by samzon Thursday, October 30, 2008 4:20 PM
    Thursday, October 30, 2008 4:19 PM

All replies

  • The easiest way I found is if you put the image in a 'Resources' folder, then right click on the image and in properties make sure it is a resource.
    Then from inside your assembly you can set:

    Source="/YourAssemblyNameHere;component/Resources/image.jpg"

    I believe this is do-able for you 'Image' folder also as: Source="/YourAssemblyNameHere;component/Images/image.jpg"

    Hope this helps!

    -noorbakhsh
    • Edited by noorbakhsh Wednesday, October 29, 2008 8:04 PM
    • Proposed as answer by noorbakhsh Wednesday, October 29, 2008 8:40 PM
    • Unproposed as answer by samzon Thursday, October 30, 2008 4:20 PM
    Wednesday, October 29, 2008 7:52 PM
  • Acutally This is what I went with:
    pack://application:,,/images/Background01.jpg 

    xaml:
    <ImageBrush x:Name="Main_Background" 
            ImageSource="pack://application:,,/images/Background01.jpg"/> 

    Code Behind:
    Uri myUri = new Uri("pack://application:,,/images/" + GetBackGroundImage() + ".jpg");  
    this.Main_Background.ImageSource = new BitmapImage(myUri); 
    • Marked as answer by samzon Thursday, October 30, 2008 4:19 PM
    • Edited by samzon Thursday, October 30, 2008 4:20 PM
    Thursday, October 30, 2008 4:19 PM
  • Just a heads up:

    That will work great if you are in main application, but if you are in an assembley that is referenced by the main app, then your code will break, since you are referencing the app and those resources are not available in the main app, but in the component!
    So just be warned in your situation it HAPPENED to work, but will not work always this way.

    -noorbakhsh
    • Edited by noorbakhsh Thursday, October 30, 2008 6:39 PM
    Thursday, October 30, 2008 6:39 PM