none
How to load image from library using ResourceLoader

    Question

  • I pulled this code from the Application resources and localization sample sample project:

    var resourceLoader = new ResourceLoader();
    var file = resourceLoader.GetFile("Images/projector.png");
    fileImage.Source = new BitmapImage(new Uri(file.Path, UriKind.Absolute));

    This works fine for images located within the main application assembly.  However, how do I load an image located within a referenced library inside my app package?  (I made sure the image's Build Action is set to Content -- same as images in the main application).

    The following resource names fail to be resolved when passed to GetFile (all throw"NamedResource Not Found.  (Exception from HRESULT: 0x80073B17"):

    "ClassLibrary1/Images/projector.png"

    "ms-resource://ClassLibrary1/Images/projector.png"

    "pack://application:,,,/ClassLibrary1;component/Images/projector.png"

    Is my syntax incorrect, or is what I'm trying to do just not supported at all?

    • Edited by nrcaliendo Friday, February 24, 2012 1:55 PM
    Friday, February 24, 2012 3:30 AM

Answers

All replies

  • Hi - I'm working on this and will update you when I know more.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator

    Tuesday, February 28, 2012 2:48 PM
    Moderator
  • Hi - I've found that embedded images are not supported in Metro.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator

    Tuesday, February 28, 2012 4:32 PM
    Moderator
  • Try:  "ms-resource://ClassLibrary1/Files/Images/projector.png"

    Tuesday, February 28, 2012 7:40 PM
  • [information on non-public build deleted

    This forum is for discussion of the publically available Developer Preview only. If you have access to a newer build under NDA then please address all questions through the channel from which you received that build.]


    Wednesday, February 29, 2012 1:49 AM
  • So, ResourceLoader.GetFile, which was in Developer Preview, is now gone from WinRT in Consumer Preview.

    I've been looking at the updated sample project, Application resources and localization sample.  Some additional scenarios have been added (e.g., loading images from AppX manifest); but none address loading images from a class library from code.  I can load strings all day long, but not images.

    It's noteworthy that only strings can be added to resource (RESW) files, but not images or anything else.  So, do "resources" = strings now in Metro?

    (BTW, "ms-resource://ClassLibrary1/Files/Images/projector.png" does not work either).
    • Edited by nrcaliendo Thursday, March 01, 2012 2:57 AM
    Thursday, March 01, 2012 2:55 AM
  • File resources are now just "files" so if you use the StorageFile API's with an "ms-appx://" URL, it will virtualize appropriately.   The ms-appx protocol only covers files, so you don't need to insert "Files" in the URI. 

    So assuming your image is in the "ClassLibrary" folder of your main package, GetFileFromApplicationUriAsync("ms-appx:///ClassLibrary/Images/projector.png") should work for you on the Consumer Preview build.   If you have multiple variants of projector.png for language, region, scale, contrast, etc. you'll get the most appropriate one for your current context.

    -- Erik

    • Proposed as answer by Robert_Levy Friday, March 02, 2012 2:37 PM
    Thursday, March 01, 2012 4:00 AM