我尝试截取 WPF 中编写的应用程序的屏幕截图,但未捕获该应用程序,我必须使用特殊工具截取屏幕截图吗?
问问题
2743 次
3 回答
6
您可以使用RenderTargetBitmap从 WPF 控件生成图像。
public const int IMAGE_DPI = 96;
public Image GenerateImage(T control)
where T : Control, new()
{
Size size = RetrieveDesiredSize(control);
Rect rect = new Rect(0, 0, size.Width, size.Height);
RenderTargetBitmap rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, IMAGE_DPI, IMAGE_DPI, PixelFormats.Pbgra32);
control.Arrange(rect); //Let the control arrange itself inside your Rectangle
rtb.Render(control); //Render the control on the RenderTargetBitmap
//Now encode and convert to a gdi+ Image object
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (MemoryStream stream = new MemoryStream())
{
png.Save(stream);
return Image.FromStream(stream);
}
}
private Size RetrieveDesiredSize(T control)
{
if (Equals(control.Width, double.NaN) || Equals(control.Height, double.NaN))
{
//Make sure the control has measured first:
control.Measure(new Size(double.MaxValue, double.MaxValue));
return control.DesiredSize;
}
return new Size(control.Width, control.Height);
}
请注意,这将生成一个 PNG 图像;)如果您希望将其存储为 JPEG,我建议您使用另一个编码器 :)
Image image = GenerateImage(gridControl);
image.Save("mygrid.png");
于 2009-08-17T11:24:51.937 回答
1
您只需按 PrtScr 按钮(Windows 会将整个桌面图像复制到缓冲区),然后将其粘贴到 Power Point 中并根据需要进行裁剪。
于 2012-01-26T13:18:13.667 回答
0
我遇到了同样的问题,我需要截屏来记录我的测试,但似乎无法到达那里。
有问题的窗口是一个无边界模态窗口,允许圆角/透明度。这是我的报告:
- HP Quality Center 的屏幕截图工具无法将其识别为窗口,因此会将其屏幕截图视为窗口不存在。
- SnagIt 使用组合键进入捕获模式。一旦击中,弹出窗口就会消失。它在捕获结束后重新出现。
- 标准 Windows 捕获工作正常 (Alt + Prt Scr) 并按预期捕获窗口。
接下来我尝试使用打开的下拉列表捕获窗口。上面提到的方法似乎都不起作用(最后一种方法以与以前相同的方式捕获 Window,没有打开的下拉菜单)。
据我正确理解所有的谈话,你唯一能做的就是在应用程序中实现它......
于 2011-03-09T08:03:33.583 回答