我有一个WinForms应用程序,我需要截取特定控件的屏幕截图。通常这很简单(DrawToBitmap)但我需要截屏的控件不支持这个(ChromiumWebBrowser / CefSharp)。因此我一直在使用以下代码截取屏幕截图:
Rectangle bounds = browser.Bounds;
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
int borderWidth = (this.Width - this.ClientSize.Width) / 2;
int titleBarHeight = this.Height - this.ClientSize.Height + toolStrip1.Height - borderWidth;
g.CopyFromScreen(browser.Left, this.Top + browser.Top + titleBarHeight, 0, 0, bounds.Size);
}
}
这会找到我需要截屏的控件的位置,并截取该控件的屏幕截图。这样可以正常工作,但是如果我将应用程序移动到另一台显示器,屏幕截图将取决于正确的区域,而是在主显示器上。
有没有更简单的方法来解决这个问题?