2

I am trying to get the width and height of the uploaded html file. This is what i have done so far.

WebBrowser wb = new WebBrowser();
int ht,wd;
wb.Navigate("D:\\page1.HTM");
ht = wb.Height;
wd = wb.Width;

But the above code always return me the wrong size of the page.

Can anyone help me to get the exact page size in the c# environment?

FYI: For those whose marked this question a duplicate, Please consider that the original question(as per your point of view), The webbrowser tool is implement in the form but in my case, I am not going to implement it. I am just going to use it. I hope the first line of code denotes that.

Posting Pro
  • 43
  • 2
  • 7
  • @pravprab , I need the **actual pixel** size of the uploaded html document. And as per your link, it says we need a parser. I used a virtual webrowser control to load the html file to meet my requirement. Do u find any other way to implement my option here? – Posting Pro Feb 11 '14 at 05:33
  • **FYI:** I need actual size only for internet explorer. I dont care about other browsers. – Posting Pro Feb 11 '14 at 05:39
  • Its a good option.. but as far i know, we cannot implement a javascript in a virtual webbrowser. :( Or is there any other methods? – Posting Pro Feb 11 '14 at 05:58

1 Answers1

3

You can try

public partial class Form1 : Form
{
    WebBrowser wb = new WebBrowser();
    public Form1()
    {
        InitializeComponent();
        wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
        wb.Navigate("D:\\page1.HTM");

    }

    private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        int scrollWidth = wb.Document.Body.ScrollRectangle.Width;
        int scrollHeight = wb.Document.Body.ScrollRectangle.Height;
    }
}

This worked fine for me ..!

the code

wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);

is Used to bind DocumentCompleted event to virtual webbrowser . Form1 is a winform and there is no control present in that form except virtual webbrowser, declared as WebBrowser wb = new WebBrowser();

DocumentCompleted event is triggered after the document (here page1.HTM) is completely loaded .

Reference

pravprab
  • 2,301
  • 3
  • 26
  • 43
  • Actually, you understood in the quite opposite way. I am having a html file in a location. If i refer that from a c# application the application must return the actual width and height of the html page (as per internet explorer). So, I cannot afford to use an ajax component in the file. – Posting Pro Feb 11 '14 at 06:32
  • I am not allowed to rewrite the html file...Eventhough it gets fired, how can i get the value in c#... i.e, from javascript to c#? – Posting Pro Feb 11 '14 at 06:58
  • Fine. If it is one file, i can rewrite the file.But, consider the the operation has to done for a set of 10000 file. In that case your suggestion fails. – Posting Pro Feb 11 '14 at 08:24
  • Its possible... But I am searching to know how... Because there are several dll which convert html files to pdf. Their conversion is mainly based on the html page size. They are developed in visual studio only. So I cant take it as impossible. – Posting Pro Feb 11 '14 at 08:40
  • Object reference not set to an instance of an object. This is the error which i got while using your suggestion. My code is WebBrowser wb = new WebBrowser(); wb.Navigate(new Uri("D:\\page1.HTM")); int scrollWidth = wb.Document.Body.ScrollRectangle.Width; int scrollHeight = wb.Document.Body.ScrollRectangle.Height; – Posting Pro Feb 11 '14 at 09:53
  • I have clearly stated that the webbrowser is virtual. So i cannot use events i hope. :( – Posting Pro Feb 11 '14 at 10:24
  • can u provide me a sample please? – Posting Pro Feb 11 '14 at 10:27
  • Are you sure that while debugging it is stepping over into **wb_DocumentCompleted** function? Because I tried its not even coming into that code. – Posting Pro Feb 11 '14 at 12:16
  • @PostingPro Put breakpoint on `int scrollWidth = wb.Document.Body.ScrollRectangle.Width;` and wait for some time to load html completely , it will step into the break point. It works fine for me...! – pravprab Feb 12 '14 at 03:28
  • I tried my friend.. Its no use :( – Posting Pro Feb 12 '14 at 03:51