Namespaces
Variants
Actions

How to capture screen programmatically in Windows Phone 7

Jump to: navigation, search

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

Article Metadata

Code Example
Tested with
SDK: Windows Phone 7.1 SDK
Devices(s): Emulator

Compatibility
Platform(s): Windows Phone 7.5

Article
Created: pavan.pareta (30 Dec 2011)
Last edited: bintk (12 May 2013)

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.

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.

This page was last modified on 12 May 2013, at 22:32.
720 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved