Solved

Resize WebBrowser to fit content

Posted on 2011-03-11
3
2,290 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with Filesystemobject or .NET version 3 36
Error in script 11 47
C# parent child form 5 19
execute SQL Stored Procedure 1000 times from vb.net 3 26
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 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

21 Experts available now in Live!

Get 1:1 Help Now