gpt4 book ai didi

c# - 截取 wpf 弹出窗口的屏幕截图

转载 作者:行者123 更新时间:2023-11-30 20:13:48 29 4
gpt4 key购买 nike

我尝试截取一个用 WPF 编写的应用程序,但应用程序没有被捕获,我是否必须使用特殊工具来截取屏幕截图?

最佳答案

您可以使用 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");

关于c# - 截取 wpf 弹出窗口的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1287373/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com