从窗口内容中截取屏幕截图(无边框)
本文关键字:边框 屏幕截图 截取 窗口 | 更新日期: 2023-09-27 17:59:11
我正在寻找如何用C#将表单内容保存在位图中的解决方案。我已经尝试过使用DrawToBitmap,但它捕获了所有带有边框的窗口。
这就是这个代码的结果:
public static Bitmap TakeDialogScreenshot(Form window)
{
var b = new Bitmap(window.Bounds.X, window.Bounds.Y);
window.DrawToBitmap(b, window.Bounds);
return b;
}
呼叫是:
TakeDialogScreenshot(this);
谁想到的:D
我已经在谷歌上搜索过了,但没能找到。谢谢!
编辑:虽然使用ClientArea
是键,但这还不够,因为DrawToBitmap
总是包括标题、边框和滚动条。。
因此,在获取全屏(或者更确切地说是"formshot")后,我们必须对其进行裁剪,使用我们可以从客户端区域的原点映射到屏幕坐标中获得的偏移量,并从已经在屏幕坐标中的表单位置中减去这些偏移量。:
public static Bitmap TakeDialogScreenshot(Form window)
{
var b = new Bitmap(window.Width, window.Height);
window.DrawToBitmap(b, new Rectangle(0, 0, window.Width, window.Height));
Point p = window.PointToScreen(Point.Empty);
Bitmap target = new Bitmap( window.ClientSize.Width, window.ClientSize.Height);
using (Graphics g = Graphics.FromImage(target))
{
g.DrawImage(b, 0, 0,
new Rectangle(p.X - window.Location.X, p.Y - window.Location.Y,
target.Width, target.Height),
GraphicsUnit.Pixel);
}
b.Dispose();
return target;
}
很抱歉在我的第一篇文章中出现错误!