Link to home
Start Free TrialLog in
Avatar of yoeddy
yoeddy

asked on

Resize WebBrowser to fit content

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
Avatar of Webmonkey
Webmonkey
Flag of United States of America image

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

Avatar of yoeddy
yoeddy

ASKER

I am, but it needs to resize itself according to the size of content in order to take the screenshot of the entire page
ASKER CERTIFIED SOLUTION
Avatar of Webmonkey
Webmonkey
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial