本文介绍了如何获取网页的截图 - 从上传的文件中挑选的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我对C#sharp coding完全不熟悉 - 我正试图拍摄网页的屏幕截图

其网址是最初从同一表格上传的记事本中拾取



当我通过MSDN中的web_browser控件阅读时...我确实到达了以下代码 - 但是当我运行时,我只有白色屏幕



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



有人可以说除了处理之外还应该注意什么呢? webbrowserdocumentcompletedeventhandler>这应该得到我想要的,因为我在MSDN中阅读。



PS:请原谅,如果我的编码风格非常错误..如上所述,我刚刚开始我的C#编码: )



我试过的代码...



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

        }

    }


}

推荐答案


这篇关于如何获取网页的截图 - 从上传的文件中挑选的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 05:19
查看更多