Search code examples
c#windows-phone-8screenshotmedia

System.UnauthorizedAccessException' occurred in Microsoft.Xna.Framework.ni.dll raised Screenshot windows phone 8


I am attempting to capture a screenshot and save it as jpeg into the MediaLibrary but received

A first chance exception of type System.UnauthorizedAccessException occurred in Microsoft.Xna.Framework.ni.dll at lib.SavePicture(filePath, ms);

My code:

public static void SaveToMediaLibrary(FrameworkElement element, string title)
{
    using (MemoryStream ms = new MemoryStream())
    {
        bmp.SaveJpeg(ms, (int)element.ActualWidth, (int)element.ActualHeight, 0, 100);
        var lib = new MediaLibrary();
        var filePath = string.Format(title + ".jpg");
        ms.Seek(0, SeekOrigin.Begin); 
        lib.SavePicture(filePath, ms);
    }
}

I am using Windows Phone 8 emulator for the test. What am I missing?


Solution

  • OK I think I figured it out. I have to add an ID_CAP_MEDIALIB_PHOTO capability to your app's manifest!