Solved

Resize WebBrowser to fit content

Posted on 2011-03-11
3
2,261 Views
Last Modified: 2013-12-17
Hi,
I'm trying to take a screenshot of some different webpages, but I need to resize my webbrowser control to fit the web page before using DrawToBitmap to create the image.

My code is

WebBrowser wb = new WebBrowser();
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(url);
while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }  
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
return bitmap;

Open in new window


I've tried various combinations of

Document.Window.Size
Document.Body.Document.Window.Size.Width

Open in new window


but it still chops bits off the sides and bottom.

Thanks
0
Comment
Question by:yoeddy
  • 2
3 Comments
 
LVL 6

Expert Comment

by:Webmonkey
Comment Utility
I don't think its the Document object that you should be looking at - I would think that is referring to the client-side element.  Try setting the height and width of the WebBrowser control.

WebBrowser wb = new WebBrowser();
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Width = 1024;
wb.Height = 768;
wb.Navigate(url);
while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }  
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
return bitmap;

Open in new window

0
 

Author Comment

by:yoeddy
Comment Utility
I am, but it needs to resize itself according to the size of content in order to take the screenshot of the entire page
0
 
LVL 6

Accepted Solution

by:
Webmonkey earned 500 total points
Comment Utility
Well, here's the problem... unless the page is assigned a height and width, some browsers aren't able to "see" the computed height and width.  You'd have to make sure that each page is assigned a height and width... even then it could be flaky.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now