This is a mirror of official site: http://jasper-net.blogspot.com/

Taking a screenshot from within a Silverlight #WP7 application

| Thursday, December 30, 2010
Often times, you want to take a screenshot of an application’s page. There can be multiple reasons. For instance, you can use this to provide an easy feedback method to beta testers. I find this super invaluable when working on integration of design in an app, and the user can take quick screenshots, attach them to an email and send them to me directly from the Windows Phone device. However, the same mechanism can also be used to provide screenshots are a feature of the app, for example if the user wants to save the current status of his application, etc.

Caveats

Note the following:

  • The code requires an XNA library to save the picture to the media library. To have this, follow the steps:
  • In your application (or class library), add a reference to Microsoft.Xna.Framework.
  • In your code, add a “using” statement to Microsoft.Xna.Framework.Media.
  • In the Properties folder, open WMAppManifest.xml and add the following capability: ID_CAP_MEDIALIB.
  • The method call will fail with an exception if the device is connected to the Zune application on the PC. To avoid this, either disconnect the device when testing, or end the Zune application on the PC.
  • While the method call will not fail on the emulator, there is no way to access the media library, so it is pretty much useless on this platform.
  • This method only prints Silverlight elements to the output image. Other elements (such as a WebBrowser control’s content for instance) will output a black rectangle.

The code

public static void SaveToMediaLibrary(FrameworkElement element, string title)
{
   try
   {
       var bmp = new WriteableBitmap(element, null);

       var ms = new MemoryStream();
       bmp.SaveJpeg(
           ms,
           (int)element.ActualWidth,
           (int)element.ActualHeight,
           0,
           100);
       ms.Seek(0, SeekOrigin.Begin);

       var lib = new MediaLibrary();
       var filePath = string.Format(title + ".jpg");
       lib.SavePicture(filePath, ms);

Read more: Laurent Bugnion (GalaSoft)

Posted via email from .NET Info

0 comments: