简体   繁体   English

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

[英]Take screenshot from window content (without border)

I am searching a solution on how to save the content of a form in a bitmap with C#. 我正在寻找有关如何使用C#在位图中保存表单内容的解决方案。 I have already tried to use DrawToBitmap, but it captures all the window with the border. 我已经尝试使用DrawToBitmap,但是它捕获了所有带有边框的窗口。

That is the result of this code: 这是此代码的结果:

public static Bitmap TakeDialogScreenshot(Form window)
 {
    var b = new Bitmap(window.Bounds.X, window.Bounds.Y);
    window.DrawToBitmap(b, window.Bounds);
    return b;
 }   

Call is: 致电是:

TakeDialogScreenshot(this);

Who thought it :D 谁以为是:D

I have already searched on google, but I did not manage to get it. 我已经在Google上搜索过,但没有设法得到它。 Thanks! 谢谢!

Edit: While using the ClientArea is key, it isn't quite enough, as DrawToBitmap will always include the title, borders, scrollbars.. 编辑:虽然使用ClientArea是关键,但这还不够,因为DrawToBitmap将始终包含标题,边框和滚动条。

So after taking the full screen- or rather 'formshot', we'll have to crop it, using an offset we can get from mapping the origin of the clientarea to the screen coordinates and subtracting these from the form location, which already is in screen coordinates..: 因此,在获取全屏或“ 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;
}

Sorry for the error in my first post! 对不起,我的第一篇文章中的错误!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM