• 【转】[wp7应用内截图]Taking a screenshot from within a Silverlight #WP7 application


    感谢@林永坚Jake老师提供链接 怕以后找不到了转过来

    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

     1 public static void SaveToMediaLibrary(
    2 FrameworkElement element,
    3 string title)
    4 {
    5 try
    6 {
    7 var bmp = new WriteableBitmap(element, null);
    8
    9 var ms = new MemoryStream();
    10 bmp.SaveJpeg(
    11 ms,
    12 (int)element.ActualWidth,
    13 (int)element.ActualHeight,
    14 0,
    15 100);
    16 ms.Seek(0, SeekOrigin.Begin);
    17
    18 var lib = new MediaLibrary();
    19 var filePath = string.Format(title + ".jpg");
    20 lib.SavePicture(filePath, ms);
    21
    22 MessageBox.Show(
    23 "Saved in your media library!",
    24 "Done",
    25 MessageBoxButton.OK);
    26 }
    27 catch
    28 {
    29 MessageBox.Show(
    30 "There was an error. Please disconnect your phone from the computer before saving.",
    31 "Cannot save",
    32 MessageBoxButton.OK);
    33 }
    34 }

    This method can save any FrameworkElement. Typically I use it to save a whole page, but you can pass any other element to it.

    • On line 7, we create a new WriteableBitmap. This excellent class can render a visual tree into a bitmap. Note that for even more features, you can use the great WriteableBitmapEx class library (which is open source).

    • On lines 9 to 16, we save the WriteableBitmap to a MemoryStream. The only format supported by default is JPEG, however it is possible to convert to other formats with the ImageTools library (also open source).

    • Lines 18 to 20 save the picture to the Windows Phone device’s media library.

    Using the image

    To retrieve the image, simply launch the Pictures library on the phone. The image will be in Saved Pictures. From here, you can share the image (by email, for instance), or synchronize it with the PC using the Zune software.

    Saving to other platforms

    It is of course possible to save to other platforms than the media library. For example, you can send the image to a web service, or save it to the isolated storage on the device. To do this, instead of using a MemoryStream, you can use any other stream (such as a web request stream, or a file stream) and save to that instead.

    Hopefully this code will be helpful to you!

    Happy coding,

    Laurent

    原帖:http://geekswithblogs.net/lbugnion/archive/2010/12/28/taking-a-screenshot-from-within-a-silverlight-wp7-application.aspx

    public void CaptureScreen(object sender, EventArgs e)
    {
      WriteableBitmap bmp = new WriteableBitmap(480, 800);
      bmp.Render(App.Current.RootVisual, null);
      bmp.Invalidate();

      MemoryStream stream = new MemoryStream();
      bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 80);
      stream.Seek(0, SeekOrigin.Begin);

      MediaLibrary library = new MediaLibrary();
      string filename = "ScreenShot_" + DateTime.Now.ToString("yyyy-MM-dd_hh:mm:ss");
      library.SavePicture(filename, stream);
      stream.Close();
    }
  • 相关阅读:
    31天重构指南之二十:提取子类
    31天重构指南之二十二:分解方法
    大叔手记(17):大叔2011年读过的书及2012年即将要读的书
    深入理解JavaScript系列(5):强大的原型和原型链
    深入理解JavaScript系列(10):JavaScript核心(晋级高手必读篇)
    深入理解JavaScript系列(11):执行上下文(Execution Contexts)
    深入理解JavaScript系列(8):S.O.L.I.D五大原则之里氏替换原则LSP
    深入理解JavaScript系列(4):立即调用的函数表达式
    深入理解JavaScript系列(3):全面解析Module模式
    深入理解JavaScript系列(7):S.O.L.I.D五大原则之开闭原则OCP
  • 原文地址:https://www.cnblogs.com/liubaicai/p/2356227.html
Copyright © 2020-2023  润新知