如何获取网页的截图 - 从上传的文件中挑选的URL [英] how to take screenshot of Webpage -while URL picked from a uploaded file..

查看:100
本文介绍了如何获取网页的截图 - 从上传的文件中挑选的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)
        {

        }
       
    }

  
}

推荐答案

听起来是什么声音就像你要做的那样,取一个预定的网站列表在本地文件系统上的文本文档中列出。

(需要确定它们在文本文档中的列出方式,每行1个,或者作为某种分隔文件)



然后使用Web浏览器控件打开每个网页。

(解析地址的文本文件行,然后使用Web浏览器控件打开并查看页面或使用某个版本的每个循环将地址添加到集合或列表然后使用该列表打开Web地址)



(确保页面已完全加载)



然后拍摄浏览器控件显示的屏幕截图。





然后将屏幕截图保存到本地文件系统。



对于列表中的每个站点。





但是上面列出的代码看起来像是在做什么。



打开文件,读取所有行,上传为1个大字符串。

无法从代码中分辨出它应该上传的位置to。



然后它会触发一个事件处理程序,如果文档已加载。在发布的代码中不清楚事件处理程序的工作原理。



并且假设采取上述截图。



你也正在捕捉导航内容的错误并且应该提供关于错误的反馈,但似乎是默默地忽略了URI格式错误





为了做你想做的事情,我可以告诉你,你需要分配更多代码。



不确定这是否有助于任何与否/>
WebBrowser.Document属性

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.document.aspx? cs-save-lang = 1& cs-lang = csharp#code-snippet-2 [ ^ ]



更新:

链接到Webbrowser示例

http://msdn.microsoft.com/en-us/library/3tst62z1(v = VS.90 ).aspx [ ^ ]



Visual Studio样本页。

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 =视觉%20Studio%202008&安培; ac = 3 [ ^ ]



希望这有所帮助。
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.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆