Perform screen-scape of Webbrowser control in thread
You can write
private Image TakeSnapShot(WebBrowser browser)
{
browser.Width = browser.Document.Body.ScrollRectangle.Width;
browser.Height= browser.Document.Body.ScrollRectangle.Height;
Bitmap bitmap = new Bitmap(browser.Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth, browser.Height);
browser.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
return bitmap;
}
A full working code
var image = await WebUtils.GetPageAsImageAsync("http://www.stackoverflow.com");
image.Save(fname , System.Drawing.Imaging.ImageFormat.Bmp);
public class WebUtils
{
public static Task<Image> GetPageAsImageAsync(string url)
{
var tcs = new TaskCompletionSource<Image>();
var thread = new Thread(() =>
{
WebBrowser browser = new WebBrowser();
browser.Size = new Size(1280, 768);
WebBrowserDocumentCompletedEventHandler documentCompleted = null;
documentCompleted = async (o, s) =>
{
browser.DocumentCompleted -= documentCompleted;
await Task.Delay(2000); //Run JS a few seconds more
Bitmap bitmap = TakeSnapshot(browser);
tcs.TrySetResult(bitmap);
browser.Dispose();
Application.ExitThread();
};
browser.ScriptErrorsSuppressed = true;
browser.DocumentCompleted += documentCompleted;
browser.Navigate(url);
Application.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
return tcs.Task;
}
private static Bitmap TakeSnapshot(WebBrowser browser)
{
browser.Width = browser.Document.Body.ScrollRectangle.Width;
browser.Height= browser.Document.Body.ScrollRectangle.Height;
Bitmap bitmap = new Bitmap(browser.Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth, browser.Height);
browser.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
return bitmap;
}
}
C# can I Scrape a webBrowser control for links?
With the HtmlAgility pack it's easy:
HtmlWindow window = webBrowser1.Document.Window;
string str = window.Document.Body.OuterHtml;
HtmlAgilityPack.HtmlDocument HtmlDoc = new HtmlAgilityPack.HtmlDocument();
HtmlDoc.LoadHtml(str);
HtmlAgilityPack.HtmlNodeCollection Nodes = HtmlDoc.DocumentNode.SelectNodes("//a");
foreach (HtmlAgilityPack.HtmlNode Node in Nodes)
{
textBox1.Text += Node.OuterHtml + "\r\n";
}
Screen scraping web page containing button with AJAX
So a really simple solution seems to have worked. My code now looks like:
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = (WebBrowser)sender;
HtmlElement expandDetails = browser.Document.GetElementById("form:SummarySubView:closedToggleControl");
//When open ID for element is "form:SummarySubView:openToggleControl"
if(expandDetails == null) //If already expanded
{
//Stuff
}
else
{
expandDetails.InvokeMember("click"); //Click on element to run AJAX
while (expandDetails != null)
{
expandDetails = browser.Document.GetElementById("form:SummarySubView0:closedToggleControl");
Application.DoEvents();
System.Threading.Thread.Sleep(200);
}
//Stuff
}
}
So running the while
loop works fine for my case.
WriteableBitmap.Render() not capturing when control not in visual tree (all black pixels instead)
This turns out to have been doomed from the start.
You can't run the WebBrowser control in the background on WP8, period. It's an Unsupported API.
And, as noted above, there's the issue (maybe not a bug?) that WebBrowser won't provide a bitmap when it's not in the visual tree, anyway.
Related Topics
Equivalent of Tuple (.Net 4) for .Net Framework 3.5
Requesting HTML Over Https with C# Webclient
Entity Framework Creates a Plural Table Name, But the View Expects a Singular Table Name
Outofmemoryexception on Declaration of Large Array
How to Change Pixel Color of an Image in C#.Net
Local Database, I Need Some Examples
C#: How to Edit Items and Subitems in a Listview
Httpwebrequest Times Out on Second Call
How to Format 07/03/2012 to March 7Th,2012 in C#
The Operation Cannot Be Completed Because the Dbcontext Has Been Disposed Error
Where Are Clr-Defined Methods Like [Delegate].Begininvoke Documented
Ef Core 2.1 Hasconversion on All Properties of Type Datetime
Passing Parameters to Xslt Stylesheet via .Net
Encoding Trouble with Httpwebresponse
Applying % Number Format to a Cell Value Using Openxml
How to Draw a Rounded Rectangle in C#
What Would Be the Fastest Way to Concatenate Three Files in C#