0

I am totally new to C# 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 documentation on the web_browser control in MSDN.. I did arrive at the following code - yet when I run, I only get 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 which should get what I want as I read in MSDN.

P.S: pls forgive if I'm terribly wrong in coding style .. as aforesaid I'm 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()
        {
            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)
        {
            //specifying sample values for image or screenshot size
            int x = 600, y = 700;
            Bitmap bitmap = new Bitmap(x, y);
            webBrowser1.DrawToBitmap(bitmap, new Rectangle(0, 0, x, y));

            //to give each screen a unique name - i append some no onto it
            temp = temp + 1;

            string TempFname = "Screenshotref" + temp.ToString() + "." + "jpg";
            bitmap.Save(TempFname);
        }
    }
}
TimothyP
  • 21,178
  • 26
  • 94
  • 142
  • 3
    Read how to indent your code. VS does this for you btw. – Dan Abramov Dec 09 '12 at 20:53
  • Good Day & Thanks for ur advice Dan - i shall read and learn to use it ASAP :)..and btw any help on my query pls? – Watson Arul Dec 09 '12 at 21:04
  • You have not posted the code for `takescreen` which is supposed to take screenshot. – Dan Abramov Dec 09 '12 at 21:19
  • 1
    Also, you're calling `NavigateContent` in a tight loop so only the last address will load. Just like with a normal browser, you'd need to wait for the page to load and the screenshot to be taken, before loading the next page. – Dan Abramov Dec 09 '12 at 21:21
  • You have an unclosed `try` block there—what are you trying to accomplish with it? – Dan Abramov Dec 09 '12 at 21:28
  • as i gone through some old stackoverflow pages on the same - i thought like handling the DocumentCompletedEvent might prevent the need to pass some time delay within the foreach loop :(.. i shall try as you adviced to add some added delays – Watson Arul Dec 09 '12 at 21:32
  • My apologies for the confusion in open Try Block - as its my first time uploading a code snippet .Just have checked all loops and functions are added onto the Code piece now :) – Watson Arul Dec 09 '12 at 21:40
  • 1
    This answer may be helpful for you: http://stackoverflow.com/a/2434231/1730086 – Nick Hill Dec 09 '12 at 22:12
  • possible duplicate of [Programmatically get screenshot of page](http://stackoverflow.com/questions/1981670/programmatically-get-screenshot-of-page) – Dante May Code Dec 10 '12 at 02:22

0 Answers0