Reputation: 21178
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
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
Reputation: 15261
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
Reputation: 55946
I believe you can get it from the following property:
myWebBrowser.Document.ScrollRectangle.Size;
Being a System.Drawing.Size
object.
Upvotes: 4