从窗口内容(无边框)截取屏幕截图

我正在寻找一个如何使用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

我已经在google上搜索了,但我没有设法得到它。 谢谢!

编辑:虽然使用ClientArea是关键,但它还不够,因为DrawToBitmap将始终包含标题,边框,滚动条。

所以在拍完全屏幕之后 – 或者更确切地说是’formhot’之后,我们将不得不裁剪它,使用我们可以通过将客户区域的原点映射到屏幕坐标并从表格位置减去这些偏移来获得的偏移量。屏幕坐标..:

 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(pX - window.Location.X, pY - window.Location.Y, target.Width, target.Height), GraphicsUnit.Pixel); } b.Dispose(); return target; } 

对不起我的第一篇文章中的错误!