×
Namespaces

Variants
Actions
Revision as of 04:30, 10 April 2013 by hamishwillee (Talk | contribs)

How to capture screen programmatically in Windows Phone 7

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to capture the screen from within your Windows Phone 7 app.

WP Metro Icon Multimedia.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
SDK: Windows Phone 7.1 SDK
Devices(s): Emulator
Compatibility
Platform(s): Windows Phone 7.5
Windows Phone 8
Windows Phone 7.5
Article
Created: pavan.pareta (30 Dec 2011)
Last edited: hamishwillee (10 Apr 2013)

Introduction

There does not appear to be any way on Windows Phone 7 to write a screen capture app to capture the UI of another app. However it is possible to capture the screen of your own app - a process which is covered in this article.

WpScreen.png

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.

661 page views in the last 30 days.
×