Solved

Resize WebBrowser to fit content

Posted on 2011-03-11
3
2,388 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
ID: 35120047
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
ID: 35150901
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
ID: 35156512
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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