Toadums
Toadums

Reputation: 2812

Reading a jpg into BMP in WP8

When I try to read in a jpg into a BitmapImage in a windows phone 8 app, and I get the following error:

System.UnauthorizedAccessException

I was reading up about it, and it told me that I need to check the Photo Capability in the app's manifest file, which I did. I still get the error.

My code to read in is:

System.Windows.Media.Imaging.BitmapImage b = 
    new System.Windows.Media.Imaging.BitmapImage(new Uri(@"cat.jpg",         
    UriKind.RelativeOrAbsolute));

Are there any other causes for this error?

Upvotes: 0

Views: 693

Answers (1)

Richard Szalay
Richard Szalay

Reputation: 84804

BitmapImage object can only be constructed on the UI thread. You can do so using Dispatcher.BeginInvoke:

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    System.Windows.Media.Imaging.BitmapImage b = 
        new System.Windows.Media.Imaging.BitmapImage(new Uri(@"cat.jpg",         
        UriKind.RelativeOrAbsolute));

});

Keep in mind that it's asynchronous, though, so your execution will need to continue within the lambda passed to BeginInvoke

Upvotes: 1

Related Questions