我正在使用中显示的技术
尝试获取网页的屏幕截图,当WebBrowser
控件放置在WinForm
. 但是,当在线程内运行时,它会通过提供桌面的任意图像而失败。
Thread browserThread = new Thread(() =>
{
WebBrowser br = new WebBrowser();
br.DocumentCompleted += webBrowser1_DocumentCompleted;
br.ProgressChanged += webBrowser1_ProgressChanged;
br.ScriptErrorsSuppressed = true;
br.Navigate(url);
Application.Run();
});
browserThread.SetApartmentState(ApartmentState.STA);
browserThread.Start();
private Image TakeSnapShot(WebBrowser browser)
{
int width;
int height;
width = browser.ClientRectangle.Width;
height = browser.ClientRectangle.Height;
Bitmap image = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(image))
{
Point p = new Point(0, 0);
Point upperLeftSource = browser.PointToScreen(p);
Point upperLeftDestination = new Point(0, 0);
Size blockRegionSize = browser.ClientRectangle.Size;
blockRegionSize.Width = blockRegionSize.Width - 15;
blockRegionSize.Height = blockRegionSize.Height - 15;
graphics.CopyFromScreen(upperLeftSource, upperLeftDestination, blockRegionSize);
}
return image;
}
这显然是由于该方法而发生的,Graphics.CopyFromScreen()
但我不知道有任何其他方法。有没有人可以建议的方法来解决这个问题?或者是我创建表单、添加控件、使其可见然后屏幕抓取的唯一选择?出于显而易见的原因,我希望避免这种方法。