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.
I believe you can get it from the following property:
myWebBrowser.Document.ScrollRectangle.Size;
Being a System.Drawing.Size
object.
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.
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;
精彩评论