Size WPF Browser to its HTML-Content

前端 未结 2 621
孤独总比滥情好
孤独总比滥情好 2021-01-25 11:34

I want to show my Flair in a WPF Browser Control and size it to the smallest size possible.

XAML:



        
相关标签:
2条回答
  • 2021-01-25 11:57

    You can set the size of browser to 0,0 before navigating and then after document load completed, you can set Width and Height of control to scrollWidth and scrollHeight of document:

    private void browser_LoadCompleted(object sender, NavigationEventArgs e)
    {
        browser.Width = ((dynamic)browser.Document).Body.parentElement.scrollWidth;
        browser.Height = ((dynamic)browser.Document).Body.parentElement.scrollHeight;
    }
    private void browser_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        browser.Width = 0;
        browser.Height = 0;
    }
    

    Note

    1) The post is an answer to Size WPF Browser to its HTML-Content. If the concern is exactly showing the image, you can rely on an Image control as mentioned also in the first comment.

    2) When showing your flair using WebBrowser control, you need to turn off scrolling by setting scroll="no" attribute for body tag. Also you need to set style=\"padding:0px;margin:0px;\" for body tag. Also remove borders of image by setting style="border:0px;" for img tag and also maybe some other html and css corrections.

    3) In a windows with DPI settings set to 125% you need to multiply values by 0.8.

    0 讨论(0)
  • 2021-01-25 12:15

    http://setmy.browsersize.com/ Does this help? This appears to be allowing you to set your browser to a set size for development purposes. I feel this might be a nudge in the right direction?

    <a href='javascript:(function(w,h){
    var n=window,d=document,s=n.screen;
    function r(o){      
        n.resizeTo(Number(w)+o,h);
        return n.innerWidth||d.body.clientWidth;
    } 
    function c(t,u){
        return t>u?(t-u)>>1:0;
    }
    if(r(1)==r(0)) { 
        if(typeof p=="undefined"||p.closed){
            $("info").className="no_popup";
            p=n.open(n.location.href,
                "resize_popup",
                "menubar,
                toolbar,
                location,
                directories,
                status,
                resizable,
                titlebar,
                ".split(",").join("=yes,")+",
                width="+w+",height="+h+",
                left="+c(s.width,w)+",
                top="+c(s.height,h));
         } else {
                p.focus();
         }
         p.resizeTo(w,h);n=p;
    }
    if(w>s.width||h>s.height){
        n.moveTo(0,0);
    }
    })(640, 480);'>640 x 480</a>
    
    0 讨论(0)
提交回复
热议问题