C# WP7:从应用程序截图

C# WP7:从应用程序截图,c#,.net,windows-phone-7,C#,.net,Windows Phone 7,我怎样才能从代码中截图?恐怕不行。如果你想要截图,你需要在外面使用类似剪贴工具的工具。检查,这似乎可以在模拟器上实现。使用WriteableBitmap从应用程序代码中截图非常简单。Laurent Bugnon在这里写了一篇好文章:下面是如何从应用程序中,从页面代码中截取屏幕快照,并将其保存到手机的图片库中。请注意,这不会捕获SysTray或AppBar: WriteableBitmap w = new System.Windows.Media.Imaging.WriteableBitmap(t

我怎样才能从代码中截图?

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

检查,这似乎可以在模拟器上实现。

使用
WriteableBitmap
从应用程序代码中截图非常简单。Laurent Bugnon在这里写了一篇好文章:

下面是如何从应用程序中,从页面代码中截取屏幕快照,并将其保存到手机的图片库中。请注意,这不会捕获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);
WriteableBitmap w=new System.Windows.Media.Imaging.WriteableBitmap(this,null);//这是您的当前页面
WriteableBitmap w2=新系统.Windows.Media.Imaging.WriteableBitmap(480800);
//SysTray空间
对于(int i=0;i<32;i++)
{
对于(int j=0;j<480;j++)
{
w2.像素[i*480+j]=-16777216;//黑色35; ff000000
}
}
//实际客户区
对于(int i=32;i<728;i++)
{
对于(int j=0;j<480;j++)
{
w2.像素[i*480+j]=w.像素[(i-32)*480+j];
}
}
//AppBar的空间
对于(int i=728;i<800;i++)
{
对于(int j=0;j<480;j++)
{
w2.像素[i*480+j]=-16777216;//黑色35; ff000000
}
}
MemoryStream ms=新的MemoryStream();
w2.SaveJpeg(ms,480800,0100);
Microsoft.Xna.Framework.Media.MediaLibrary lib=新的Microsoft.Xna.Framework.Media.MediaLibrary();
ms.Position=0;
lib.SavePicture(“屏幕截图”,ms);

从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);
}}

谢谢从你的链接工作的解决方案:它保存为JPEG,即使质量是100%,它实际上是相当糟糕的文物丢失。。。这似乎不是一种导出为PNG的方式吗?别忘了处理你的MemoryStream!使用using语句。我工作得很好,但是你知道如何拍摄web浏览器控件的屏幕截图吗。