WP7:从应用程序截图

本文关键字:应用程序 WP7 | 更新日期: 2023-09-27 17:48:57

我如何从代码截图?

WP7:从应用程序截图

使用WriteableBitmap从应用程序代码中截取应用程序的屏幕截图非常简单。Laurent Bugnion在这里写得很好:http://geekswithblogs.net/lbugnion/archive/2010/12/28/taking-a-screenshot-from-within-a-silverlight-wp7-application.aspx

检查这里,似乎在模拟器上是可能的。

从Silverlight #WP7应用程序中截图

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);
    MessageBox.Show(
        "Saved in your media library!",
        "Done",
        MessageBoxButton.OK);
}
catch
{
    MessageBox.Show(
        "There was an error. Please disconnect your phone from the computer before saving.",
        "Cannot save",
        MessageBoxButton.OK);
}}

这是如何从你的应用程序中截取屏幕截图,从你的页面代码,并将其保存到你的手机图片库。注意,这不会捕获SysTray或AppBar:

WriteableBitmap w = new System.Windows.Media.Imaging.WriteableBitmap(this, null); // 'this' is your current page
WriteableBitmap w2 = new System.Windows.Media.Imaging.WriteableBitmap(480, 800);
// space for SysTray
for (int i = 0; i < 32; i++)
{
    for (int j = 0; j < 480; j++)
    {
        w2.Pixels[i * 480 + j] = -16777216; // black #ff000000
    }
}
// actual client area
for (int i = 32; i < 728; i++)
{
    for (int j = 0; j < 480; j++)
    {
        w2.Pixels[i * 480 + j] = w.Pixels[(i - 32) * 480 + j];
    }
}
// space for AppBar
for (int i = 728; i < 800; i++)
{
    for (int j = 0; j < 480; j++)
    {
        w2.Pixels[i * 480 + j] = -16777216; // black #ff000000
    }
}
MemoryStream ms = new MemoryStream();
w2.SaveJpeg(ms, 480, 800, 0, 100);
Microsoft.Xna.Framework.Media.MediaLibrary lib = new Microsoft.Xna.Framework.Media.MediaLibrary();
ms.Position = 0;
lib.SavePicture("screenshot", ms);

恐怕不行。如果你想要截图,你需要在外面使用剪辑工具。