Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an image file in WinRT

I have a WinRT project, and am getting an error when trying to preview an image. I have set capabilities to allow access to the Pictures library, and am using the following code:

 var file = await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync(path);
 var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
 var img = new BitmapImage();
 img.SetSource(fileStream);

This error occurs on the first line:

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

Additional information: Error HRESULT E_FAIL has been returned from a call to a COM component.

I've tried other operations, such as folder.GetFilesAsync() with the same error. Is there another or capability that I need to allow this functionality to work correctly?

EDIT:

Based on @L.T.s answer, I tried some other capabilities. The following gives me the same error:

var folder = KnownFolders.PicturesLibrary;            
var files = await folder.GetFilesAsync();

However (obviously, providing I provide the Music capability) this does not:

var testfolder = KnownFolders.MusicLibrary;
var files = await testfolder.GetFilesAsync();

I don't doubt that this it something specific to my Pictures library, but I have no idea what that could be.

like image 672
Paul Michaels Avatar asked Mar 26 '26 01:03

Paul Michaels


1 Answers

If your cost is just to preview an image. You can use this

        Uri uri = new Uri("ms-appx:///Assets/test.png");
        BitmapImage bitmap = new BitmapImage(uri);
        Image image = new Image();
        image.Source = bitmap;

and add the image to a canvas. But you need to add your test.png to your projects assets first or you can change the Uri to the location of your test image.

like image 51
pandeSai Avatar answered Mar 28 '26 13:03

pandeSai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!