Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,

I am totally new to C# sharp coding - i am trying to take screen shots of webpages
whose URLs are initially picked up form a notepad uploaded on the same form

As i read through the web_browser control in MSDN.. i did arrived at following code - yet when i run, i get only white screens

> i tried uploading a .txt with (google/yahoo URLs as test data in 2 lines)

can someone please say what more should i take care apart from handling <webbrowserdocumentcompletedeventhandler> which should get what i want as i read in MSDN.

P.S: pls forgive if im terribly wrong in coding style .. as aforesaid im just starting my C# coding :)

Code that i tried...

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MSDN_wbc_tut1
{
    public partial class Form1 : Form
    {

        public int temp = 0;
        public Form1()
        {
            InitializeComponent();
        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void FileUploadButton1_Click(object sender, EventArgs e)
        {
            
            //once a file is uploaded i want Pgm to read contents & browse them as URLS

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.CheckFileExists = true;
            openFileDialog.AddExtension = true;
            openFileDialog.Multiselect = true;
            //Filtering for Text files alone
            openFileDialog.Filter = "text files (*.txt)|*.txt";

            //if file is selected we must then do our coding part to proceed hecneforth 

            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            { 
            //my code to say pgm wat to do once upload done...

                //Getting my Chosen file's name 
         string Fileuploaded = openFileDialog.FileName;

                //Read all line opens files - reads till EOF & closes ,prevents a while condition
                string[] FileuploadedContent = System.IO.File.ReadAllLines(Fileuploaded);

                foreach (string s in FileuploadedContent)
                {
                    NavigateContent(s);
                }
            }
        }

        private void NavigateContent(string lineasurl)
        {
             // Add an event handler that images the document after it loads.
   
            try 
            {
                webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(takescreen);
                webBrowser1.Navigate(new Uri(lineasurl));
                //webBrowser1.Navigate(lineasurl); also works
                                
            }

            catch (System.UriFormatException)
            {
                return;
            }
        
        }


        private void takescreen(object sender,WebBrowserDocumentCompletedEventArgs e)
            {

            int x = 600, y = 700;
            Bitmap bitmap = new Bitmap(x, y);
            webBrowser1.DrawToBitmap(bitmap, new Rectangle(0, 0, x, y));
            temp = temp + 1;

            string TempFname = "Screenshotref"+temp.ToString()+ "." + "jpg";
            bitmap.Save(TempFname);
        
        
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }
       
    }

  
}
Posted
Updated 9-Dec-12 10:23am
v2

1 solution

What is sounds Like you are trying to do is, take a predetermined list of websites that are listed in a text documenton on the local file system.
(Need to determine How they are listed in the Text document,1 per line,or as a Delimited file of some sort)

Then Using a Web browser control Open each web page.
(parse a line of the Text file for the Address then use the Web browser control to open and view the page or use some version of a For each loop to add the Addresses to a Collection Or list Then Use that List for the web Addresses to open)

(Make sure the Page has Loaded Completly)

And then Take A screen shot Of what the Browser Control Is Displaying.


Then Save the Screen shot to the Local File system.

For each Site on the List.


But what the Code Listed Above Looks like it is doing is.

Open File, read all lines, upload as 1 big "String".
Can't tell from the code Where it is supposed to "Upload" to.

Then It Triggers a Event Handler For if the Document Is loaded. Not clear in posted code on how the event handler works.

And Supposed to take the said Screenshot.

Also you are catching The Error for Navigate Content And should probly Be giving feedback about the Error but appears to be silently ignoring the URI Format Error


Best I can tell in order to do what you want then you will need allot more code.

Not sure if this will help any or not
WebBrowser.Document Property
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.document.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2[^]

Updated:
Link to Webbrowser Sample
http://msdn.microsoft.com/en-us/library/3tst62z1(v=vs.90).aspx[^]

Visual Studio Samples Page.
http://code.msdn.microsoft.com/vstudio/site/search?query=Webbrowser&f%5B1%5D.Value=Webbrowser&f%5B1%5D.Type=SearchText&f%5B0%5D.Value=9.0&f%5B0%5D.Type=VisualStudioVersion&f%5B0%5D.Text=Visual%20Studio%202008&ac=3[^]

Hope this help some.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900