Executar screen-scape do controle Webbrowser no thread
Eu estou usando a técnica mostrada em
Controle WebBrowser em um novo segmento
Tentando obter um rascunho de tela de uma página da Web, consegui que o código a seguir funcionasse com êxito quando oWebBrowser
controle é colocado em umWinForm
. No entanto, ele falha ao fornecer uma imagem arbitrária da área de trabalho quando executada dentro de um thread.
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;
}
Isso obviamente acontece por causa do métodoGraphics.CopyFromScreen()
mas não tenho conhecimento de nenhuma outra abordagem. Existe uma maneira de resolver esse problema que alguém poderia sugerir? ou é a minha única opção para criar um formulário, adicionar o controle, torná-lo visível e, em seguida, raspe a tela? Por razões óbvias, espero evitar essa abordagem.