0

我对 C# 编码完全陌生 - 我正在尝试截取网页的屏幕截图,这些网页的 URL 最初是从在同一表单上上传的记事本中提取的。

当我阅读web_browserMSDN 中有关控件的文档时。我确实得到了以下代码 - 但是当我运行时,我只得到白屏

我尝试上传一个.txt(谷歌/雅虎网址作为两行测试数据)

有人可以说除了处理我在 MSDN 中读到的应该得到我想要的东西之外,我还应该注意什么。

PS:如果我在编码风格上大错特错,请原谅..如前所述,我刚刚开始我的 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)
        {
            //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);
        }
    }
}
4

0 回答 0