How to capture screen programmatically in Windows Phone 7
This article explains how to capture the screen from within your Windows Phone 7 app.
Article Metadata
Code Example
Tested with
Compatibility
Article
Introduction
There does not appear to be any way on Windows Phone 7 to write a screen capture application to capture the UI of another application. However it is possible to capture the screen of your own application - a process which is covered in this article.
Implementation
We use WriteableBitmap to capture the screen. Its Render method takes two parameters, UIElement and Transform. We specify the LayoutRoot (the root element for the UI defined in XAML) as the UIElement which renders the whole UI into the bitmap. The second parameter is the MatrixTransform which is applied to elements before they are drawn into the bitmap - we specify an empty transform in this case so that elements are drawn as they would be to the device screen.
Here is the code snippet:
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
var fileName = String.Format("WmDev_{0:}.jpg", DateTime.Now.Ticks);
WriteableBitmap bmpCurrentScreenImage = new WriteableBitmap((int)this.ActualWidth, (int)this.ActualHeight);
bmpCurrentScreenImage.Render(LayoutRoot, new MatrixTransform());
bmpCurrentScreenImage.Invalidate();
SaveToMediaLibrary(bmpCurrentScreenImage, fileName, 100);
MessageBox.Show("Captured image " + fileName + " Saved Sucessfully", "WmDev Capture Screen", MessageBoxButton.OK);
currentFileName = fileName;
}
public void SaveToMediaLibrary(WriteableBitmap bitmap, string name, int quality)
{
using (var stream = new MemoryStream())
{
// Save the picture to the Windows Phone media library.
bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, quality);
stream.Seek(0, SeekOrigin.Begin);
new MediaLibrary().SavePicture(name, stream);
}
}
After rendering all the UIElements on the WriteableBitmap, we call the Invalidate() method to force it to redraw its contents and display properly.Then the capture image is saved in media library using the SaveToMediaLibrary() method which takes three parameters WriteableBitmapObject, filename, quality of JPEG photo.

