谁能提供我在 WPF 应用程序中捕获给定窗口的代码?我已经在 Windows 窗体应用程序中实现了它,但它在 WPF 中变得混乱。
问问题
100 次
1 回答
1
如果您的意思是要保存窗口的图像,您可以创建一个 RenderTargetBitmap 对象并将窗口渲染到它。
/// <summary>
/// Captures a controls visual and saves it to the file system.
/// </summary>
/// <param name="visual">A reference to the <see cref="Visual"/> that you want to capture.</param>
/// <param name="fileName">The file name that you want the image saved as.</param>
void CaptureImage(Visual visual, string fileName)
{
if (File.Exists(fileName))
File.Delete(fileName);
using (FileStream fileStream = new FileStream(fileName, FileMode.CreateNew))
{
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)Width, (int)Height, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(this);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
encoder.Save(fileStream);
}
}
与线一起使用。
CaptureImage(this, @"c:\temp\screencap.png");
于 2013-09-11T14:43:46.350 回答