WP7: returning an application from Tombstone state causes the application to crash. Using Writeablebitmap

I am using Writeablebitmap to take a screenshot of a user interface element. The code is as follows:

    private void Screenshot(FrameworkElement element, String fileNameLoader)
    {
        try
        {
            WriteableBitmap bmp = new WriteableBitmap(element, null);

            MemoryStream ms = new MemoryStream();
            bmp.SaveJpeg(ms, (int)element.ActualWidth, (int)element.ActualHeight, 0, 100);
            ms.Seek(0, SeekOrigin.Begin);

            MediaLibrary lib = new MediaLibrary();
            String filePath = string.Format(fileNameLoader);
            lib.SavePicture(filePath, ms);                
         }
        catch (Exception exception)
        {
            txtDebug.Text = "There was an error. Could not save. " + exception.ToString();
        }
    }

The problem is that if I press the save button, which calls the Screenshot () method, click the "Home" button to apply the tombstone to the application, and finally, click the "Back" button to return the application, I get a screen that says "Resume ..." and the application eventually crashes. After some debugging, I noticed that the error seems to be caused by the following line of code:

WriteableBitmap bmp = new WriteableBitmap(element, null);

Replacing this line:

    WriteableBitmap bmp = null;

saves me from crashing, but my application is not working properly (screenshot does not work).

- - , ? , .

+3
1

, :

private void Screenshot(FrameworkElement element, String fileNameLoader)
{
    WriteableBitmap bmp = new WriteableBitmap(element, null);
    using (MemoryStream ms = new MemoryStream())
    {
        bmp.SaveJpeg(ms, (int)element.ActualWidth, (int)element.ActualHeight, 0, 100);
        ms.Seek(0, SeekOrigin.Begin);
        using (MediaLibrary lib = new MediaLibrary())
        {
            String filePath = string.Format(fileNameLoader);
            lib.SavePicture(filePath, ms);
        }
    }
}
+1

All Articles