Click here to Skip to main content
12,559,073 members (47,419 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Controls Web Browser
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...

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()

        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)
        private void NavigateContent(string lineasurl)
             // Add an event handler that images the document after it loads.
                webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(takescreen);
                webBrowser1.Navigate(new Uri(lineasurl));
                //webBrowser1.Navigate(lineasurl); also works
            catch (System.UriFormatException)

        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";
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
Posted 9-Dec-12 9:28am
Updated 9-Dec-12 10:23am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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[^]

Link to Webbrowser Sample[^]

Visual Studio Samples Page.[^]

Hope this help some.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161026.1 | Last Updated 9 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100