Keith Adler
Keith Adler

Reputation: 21178

Detect actual size of HTML content in the WebBrowser control

Is there any way to detect the contents height/width in the WebBrowser control? I'd like to save the contents to an image but only at the size of the actual contents.

Thank you in advance.

Upvotes: 4

Views: 5274

Answers (3)

bh_earth0
bh_earth0

Reputation: 2818

loop elements select max height/width. i used this after documentCompleted event

    //fit to content  -get size **after documentCompleted**
    var wbAll = webBrowser.Document.All.Cast<HtmlElement>();
    var maxWidth = wbAll.Max(x => Math.Max(x.ClientRectangle.Width, x.ScrollRectangle.Width));
    var maxHeight =wbAll.Max(x => Math.Max(x.ClientRectangle.Height, x.ScrollRectangle.Height));

    webBrowser.Height = maxHeight;
    webBrowser.Width = maxWidth;

Upvotes: 0

Sheng Jiang 蒋晟
Sheng Jiang 蒋晟

Reputation: 15261

The document recalculate the layout each time the size of the browser window changes, so it does not really have a fixed size.

You can using a Golden section search to find the minimal window size large enough to contain the document, but it is a CPU-intensive task.

Upvotes: 1

Aren
Aren

Reputation: 55946

I believe you can get it from the following property:

myWebBrowser.Document.ScrollRectangle.Size;

Being a System.Drawing.Size object.

Upvotes: 4

Related Questions