Screenshot of your Windows Phone 7 App (on device)

Have you ever wonder, if it is possible to take screenshot from your Windows Phone 7 device without jailbreak?

It could be very useful, specially in applications using built in camera, accelerometer (for ignite special actions) or multitouch gestures (if you haven’t got multitouch Windows 7 device).

I prepared workaround…

Do you remember WriteableBitmap class and its Render() method, which could render any UI element for bitmap? I’ve used it to render whole PhoneApplicationPage as my screenshot in this code segment:

if (sender is PhoneApplicationPage)
{
    var page = sender as PhoneApplicationPage;
    var bmp = new WriteableBitmap((int) page.ActualWidth,
                                  (int) page.ActualHeight);
    bmp.Render(page, null);
    bmp.Invalidate();
}

Of course it is implemented in PhoneApplicationPage event.

After that we need to save our image to gallery – the easiest way for download it to our PCs.

First of all – some IsolatedStorage:

 

const string tempJpeg = "TempJPEG";

var myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists(tempJpeg))
{
    myStore.DeleteFile(tempJpeg);
}

using (var myFileStream = myStore.CreateFile(tempJpeg))
{
    bmp.SaveJpeg(myFileStream, bmp.PixelWidth, bmp.PixelHeight, 0, 85);
}

Next – WP7 method to save it in media library (class MediaLibrary form XNA – you must add reference Microsoft.Xna.Framework):

You can also use SavePictureToCameraRoll() method instead of SavePicture() method – it save picture in camera roll album instead of saved pictures album.

using (var myFileStream = myStore.OpenFile(tempJpeg,
                                           FileMode.Open,
                                           FileAccess.Read))
{
    var library = new MediaLibrary();

    library.SavePicture("SavedPicture.jpg", myFileStream);
    MessageBox.Show("Image saved to saved pictures album");

    //Optional code - see what you've done 🙂
    var pt = new PhotoChooserTask();
    pt.Show();
}

I thought that the best way to implement screenshot in PhoneApplicationPage is make a behavior, so I packed all that code in one class – ScreenShotBehavior : Behavior<PhoneApplicationPage> (System.Windows.Interactivity reference is necessary here). In final version screen shot action is on PhoneApplicationPage double tap event and behavior is avilable only after build with DEBUG constant.

In XAML you should add only this:

<phone:PhoneApplicationPage ...> <i:Interaction.Behaviors> <local:ScreenShotBehavior /> </i:Interaction.Behaviors>

... </phone:PhoneApplicationPage>

That is all – you can download entire solution from here:

http://bit.ly/znSvNE

 

And remember – No to ACTA!

NoToActaExport

Advertisements

Beginning

Hey everyone!

My name is Paweł Żochowski and I just begun writing my blog.

I’m Microsoft Student Partner from Poland, interested in natural user interfaces and mobile applications.

After work I’m sailing and training Karate Uśmiech

At this blog I’m going to write about interesting things from my everyday work as developer and trainer.

 

PICT0007

Enjoy!