locked
IRandomAccessStream from ms-appx:///Assets/somefile RRS feed

  • Question

  • I need to get an IRandomAccessStream for images in the project's Assets dir. I'm trying to templatize multiple splashes that cycles through those that exist. (i.e. one of our games could have SplashScreen1.png - SplashScreen3.png, another may have 1-6, etc.)

    I think the path is ms-appx:///Assets, so for example I'd want to open ms-appx:///Assets/SplashScreen1.png

    How do I do this? I've spent hours digging through the APIs, trying to figure out how to do this basic file operation and I'm at a loss.

    Friday, December 7, 2012 10:59 PM

Answers

  • Clearly there's a physical law which states that the moment you ask a question after dozens of failed searches, your next search will have the answer:

    http://social.msdn.microsoft.com/Forums/en-IE/winappswithcsharp/thread/e6b0da5c-7ac0-4cff-8015-a9b781bf9ccb

    The useful code (credit to Filip Skakun) was:

    var writeableBitmap = await LoadBitmap("Assets/Logo.png");
    
    public static async Task<WriteableBitmap> LoadBitmap(string relativePath)
    {
        var storageFile = await Package.Current.InstalledLocation.GetFileAsync(relativePath.Replace('/', '\\'));
        var stream = await storageFile.OpenReadAsync();
        var wb = new WriteableBitmap(1, 1);
        wb.SetSource(stream);
        return wb;
    }

    • Marked as answer by combinatorial Friday, December 7, 2012 11:13 PM
    Friday, December 7, 2012 11:13 PM

All replies

  • Clearly there's a physical law which states that the moment you ask a question after dozens of failed searches, your next search will have the answer:

    http://social.msdn.microsoft.com/Forums/en-IE/winappswithcsharp/thread/e6b0da5c-7ac0-4cff-8015-a9b781bf9ccb

    The useful code (credit to Filip Skakun) was:

    var writeableBitmap = await LoadBitmap("Assets/Logo.png");
    
    public static async Task<WriteableBitmap> LoadBitmap(string relativePath)
    {
        var storageFile = await Package.Current.InstalledLocation.GetFileAsync(relativePath.Replace('/', '\\'));
        var stream = await storageFile.OpenReadAsync();
        var wb = new WriteableBitmap(1, 1);
        wb.SetSource(stream);
        return wb;
    }

    • Marked as answer by combinatorial Friday, December 7, 2012 11:13 PM
    Friday, December 7, 2012 11:13 PM
  • if you want to use this notation ms-appx:///Assets/SplashScreen1.png you can use:

     StorageFile file =  await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/SplashScreen1.png"));


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Friday, December 7, 2012 11:32 PM