Taking screen shots from your Windows phone device is still not possible even after the mango update. This hurts developers more than anybody else. A developer from Metroapps just brought up a tiny ye nifty code that could help the developers immensely. More from the blog :
“So my solution follows that concept… I added a bit of code into the Application Deactivate event handler to take the root visual element, render it to a writable bitmap, then save it into the Picture library.
What this effectively means is that when ever you want to take a screen shot of your app, you simply hit the Windows button, at which point the code executes, saves the screenshot, then exits your app. Then, you simply hit back to get back to your app and go to another section. You can repeat this process many times to get many screenshots.”
So basically, everytime a user deactivates the application – i.e – presses the Windows button (Application_Deactivated specific), instead of back button, the root visual element gets rendered on the writeablebitmap which finally gets saved in the media library and a user can access it anytime. The followup code is using the deactivated event handler, I suggest you use an application bar and code the following in the click event handler to avoid filling user’s library with your application’s screenshot.
// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
// get a writable bitmap ready
WriteableBitmap wbmp = new WriteableBitmap(480, 800);
// set up a transform with no scale (ie 1x scale both sides)
ScaleTransform transform = new System.Windows.Media.ScaleTransform();
transform.ScaleX = 1;
transform.ScaleY = 1;
// render the application root into the bitmap
// create a memory stream, save the bitmap as a JPEG byte stream into it
MemoryStream stream = new MemoryStream();
wbmp.SaveJpeg(stream, wbmp.PixelWidth, wbmp.PixelHeight, 0, 100);
// setup the media library, save the JPEG bytes under a unique file name
MediaLibrary library = new MediaLibrary();
library.SavePicture(Guid.NewGuid() + “.jpg”, stream.ToArray());
I went ahead and tried the code and I can tell you – it works quite well! (don’t ask why there is a lonely texblock there, I have no idea)
I am using PhotoChooser task to import the saved images and as you can see above – the screen shot was saved and I was able to import the screenshot into the application and display the screenie via Imageelement. Quick example to show you that it works.
If you use an application bar – it’s a no-brainer to implement this nifty code. Let’s see how developers respond to this until Microsoft comes up and natively integrates screenshot taking ability.