开发者

Detect actual size of HTML content in the WebBrowser control

开发者 https://www.devze.com 2022-12-28 00:59 出处:网络
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.

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号