How to Upload Files Using Ajax to ASP.NET MVC Controller Action

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



Leave a reply



Submit