从窗口内容获取屏幕截图(无边框) [英] Take screenshot from window content (without border)

查看:224
本文介绍了从窗口内容获取屏幕截图(无边框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索有关如何使用C#将表单内容保存在位图中的解决方案。
我已经尝试使用DrawToBitmap,但是它捕获了所有带有边框的窗口。

I am searching a solution on how to save the content of a form in a bitmap with C#. I have already tried to use DrawToBitmap, but it captures all the window with the border.

这是此代码的结果:

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

I已经在Google上进行搜索,但是我没有设法得到它。谢谢!

I have already searched on google, but I did not manage to get it. Thanks!

推荐答案

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

While using the ClientArea is key, it isn't quite enough, as DrawToBitmap will always include the title, borders, scrollbars..

在全屏或 formshot中,我们必须使用从将客户区域的原点映射到屏幕坐标并将其从已经在屏幕坐标中的表单位置中减去的偏移量进行裁剪。 。:

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..:

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!

这篇关于从窗口内容获取屏幕截图(无边框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆