4

I would like to display the content of a picture stored in a StorageFile through a binding, but whatever I'm trying to do it doesn't seem to work.

Here are the two solutions I have already tested :

string img =( await CompetencesFolder.GetFileAsync(FormatImageNameToFileName(imageName))).Path;

And then returning the path obtained (a full absolute path to the file) to the source Property through binding, and :

 BitmapImage img = await LoadImage(await CompetencesFolder.GetFileAsync(FormatImageNameToFileName(imageName)));

  private static async Task<BitmapImage> LoadImage(StorageFile file)
        {
            BitmapImage bitmapImage = new BitmapImage();
            FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);

            bitmapImage.SetSource(stream);


            return bitmapImage;

        }

Returning the final bitmapImage later to my binded property.

None of these methods works ..

Do anyone have an idea?

EDIT : FIX

Here is the code that solved the problem :

BitmapImage img = new BitmapImage() { UriSource = new Uri( LOCAL_REPOSITORY.Path + "/Assets/gfx/cards/" + FormatImageNameToFileName(imageName) + ".jpg", UriKind.RelativeOrAbsolute) };

I did a mix of the 2 samples above : I created my bitmapImage from the absolute URI of the picture (LOCAL_REPOSITORY contains a reference to the local storage : ApplicationData.Current.LocalFolder)

I still can't figure why the 2 other ways failed : I usually bind my image directly to an Uri, a string or a BitmapImage, and it works ..

Thomas KiTe Trentin
  • 600
  • 1
  • 5
  • 19
  • What errors do you get? – JP Alioto Feb 14 '13 at 21:25
  • I didn't get any : the picture wasn't displayed without any crash nor error raised. I solved the problem (see the edit), but still I can't figure why the 2 others samples didn't work. – Thomas KiTe Trentin Feb 14 '13 at 21:33
  • Were the images located in the local folder for the other samples? Store apps do not have arbitrary access to the file system. – JP Alioto Feb 14 '13 at 21:40
  • The files were located at the exact same place throughout all my tests. I was able to get them using StorageFolder / StorageFile, but unable to display them no mater what I tried. These files are downloaded from a server of mine, and are stored in the LocalFolder. – Thomas KiTe Trentin Feb 14 '13 at 21:45

1 Answers1

10

The code below shows how you can use the loadimage method in an app: create a blank app, add an Image and a Button to the main page.

    private async void  Button_Click_1(object sender, RoutedEventArgs e)
    {
        // load file from document library. Note: select document library in capabilities and declare .png file type
        string filename = "Logo.png";
        Windows.Storage.StorageFile sampleFile = await Windows.Storage.KnownFolders.DocumentsLibrary.GetFileAsync(filename);
        // load file from a local folder
        //Windows.Storage.StorageFile sampleFile = sampleFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Assets\\Logo.png");

        BitmapImage img = new BitmapImage();
        img = await LoadImage(sampleFile);
        myImage.Source = img;
    }

    private static async Task<BitmapImage> LoadImage(StorageFile file)
    {
        BitmapImage bitmapImage = new BitmapImage();
        FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);

        bitmapImage.SetSource(stream);

        return bitmapImage;

    }
Zhiming Xue
  • 352
  • 3
  • 4
  • 1
    add checking of null pointer Exceptions (ex : file.OpenAsync in LoadImage) – Laurent Russier Sep 16 '15 at 09:36
  • Hi man, one question... Is posible convert base64 string to StorageFile? I want to attach a file generated in base64... see please the current code http://screencast.com/t/N0Sr2G3nJ – jdnichollsc Jan 08 '16 at 22:10