为了编写应用程序的用户手册,我需要从Microsoft Surface模拟器中获取一些截图。
我该怎么做?当然,我可以在我的操作系统中制作截图,然后在照片编辑器中剪切图像,但是不是更简单的方法吗?
答案 0 :(得分:2)
所以最后我找到了一个很好的方法:
class ScreenshotTaker
{
public static void TakeScreenshot(Visual target)
{
String fileName = "Screenshot-" + DateTime.UtcNow.ToString().Replace(" ", "-").Replace(".", "_").Replace(":", "_") + ".tiff";
Console.WriteLine("Try to take screenshot: " + fileName);
FileStream stream = new FileStream(fileName, FileMode.Create);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(GetScreenShot(target)));
encoder.Save(stream);
stream.Flush();
stream.Close();
Console.WriteLine("Screenshot taken");
}
private static BitmapSource GetScreenShot(Visual target)
{
Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
RenderTargetBitmap bitmap = new RenderTargetBitmap(1024, 768, 96, 96, PixelFormats.Pbgra32);
DrawingVisual drawingvisual = new DrawingVisual();
using (DrawingContext context = drawingvisual.RenderOpen())
{
context.DrawRectangle(new VisualBrush(target), null, new Rect(new Point(), bounds.Size));
context.Close();
}
bitmap.Render(drawingvisual);
return bitmap;
}
}