Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to capture screen programmatically in Windows Phone 7

From 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): Nokia Lumia 710, 800
CompatibilityArticle
Created: pavan.pareta (30 Dec 2011)
Last edited: hamishwillee (17 Jul 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 17 July 2013, at 05:06.
621 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×