Resize a WebBrowser control based on the Website page

Posted on 2014-01-24
Last Modified: 2014-02-08

Is there a way to resize a WebBrowser control (which in turn will resize a WinForm or WPF Window) based on a default size of a webpage.   For example if I click on a link in IE it will bring up a pop with a different size and width then my web browser.   I would like to make a quick app that will directly open this pop and will re-size itself to the webpage.   I hope this makes sense.   Thanks.

Question by:davidst98
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 18

Expert Comment

by:Matthew Kelly
ID: 39807863
There is no such thing as a default size of a webpage. You would need to know what content piece you were trying to size to on the page which would require knowing exactly what pages you are navigating to and what elements to look for.

Take this EE page for example. You may say, I want it to size so I see only the right and left hand navigation with everything else on either side of it cut off.

So there is a "bodyWrapper" id DIV you could size to, but that is encapsulated in other div containers that are set to 100%.

Web pages are designed to size to the browser, not the other way around. If your WebBrowser control is only setup to go to webpages you control you could do it by getting the width/height of a container object on the page.

Author Comment

ID: 39808029
Hi Matthew,  thanks for the response.  I'm a little confused.  How do pop web pages are able to change the size of the web browser and also remove menu and toolbars?

LVL 12

Expert Comment

ID: 39808035
LVL 18

Accepted Solution

Matthew Kelly earned 500 total points
ID: 39808119
Ok, this needs a lot more work, such as setting a maximum width/height etc, but here is a general concept that will resize a webbrowser control to the size of a webpage based on elimination of scroll bar space.

namespace WindowsFormsApplication2
    public partial class Form1 : Form
        public Form1()
            webBrowser1.Navigate(new Uri(""));


        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            if (HasHorizontalScrollbar)
                webBrowser1.Size = new Size(webBrowser1.Document.Body.ScrollRectangle.Width, webBrowser1.Document.Body.ScrollRectangle.Height);
                Form1.ActiveForm.Size = new Size(webBrowser1.Document.Body.ScrollRectangle.Width, webBrowser1.Document.Body.ScrollRectangle.Height);

        public bool HasHorizontalScrollbar
                var width1 = webBrowser1.Document.Body.ScrollRectangle.Width;
                var width2 = webBrowser1.Document.Window.Size.Width;

                return width1 > width2;

Open in new window


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question