用Webbrowser控件打开一个网页后,如何用Picturebox读取网页里的图片啊?   
  我想用Picturebox显示网页中的验证码,但是不知道怎么处理。 
比如说有个Webbrowser 控件名字是:Webbrowser1
Picturebox控件名字是:Picturebox1
希望高手给小弟直接代码,小弟不是很懂的.
所以恳求高手能多给予代码注解.谢谢
谢谢

解决方案 »

  1.   

    在读取的的页面中查找<img> 标记,然后读出里面的src然后就能在Picturebox里显示了.
      

  2.   

    他的SRC地址是不固定的,就像QQ的验证码图片地址
    http://ptlogin2.qq.com/getimage?aid=11000101&'+ Math.random();
    最后是Math.random
    没办法获取的地址的
    有其它方法吗?
      

  3.   

    vb.net代码,自己翻译Imports mshtmlDim bmp As Bitmap
                            Try
                                Dim img As IHTMLImgElement = HTMLDocument.all.item("Image1", Nothing)
                                Dim rang As IHTMLControlRange = HTMLDocument.body.createControlRange
                                rang.add(img)
                                rang.execCommand("Copy", False, Nothing)
                                bmp = Clipboard.GetDataObject().GetData(DataFormats.Bitmap)
                            Catch ex As Exception                        End Try
      

  4.   

    忘记定义了
    Dim HTMLDocument As IHTMLDocument2 = CType(AxWebBrowser1.Document, IHTMLDocument2)
      

  5.   

    我只是个新手呢,翻译不来
    Dim bmp As Bitmap
    这种格式什么含义啊?
    定义bmp为Bitmap?
    c#中怎么说啊..不知道了
      

  6.   

    mshtml.IHTMLDocument2 HTMLDocument = (mshtml.IHTMLDocument2)this.axWebBrowser1.Document;
    Bitmap bmp = null;
    mshtml.IHTMLImgElement img = (mshtml.IHTMLImgElement)HTMLDocument.all.item("Image1", null);
    mshtml.IHTMLControlRange rang = (mshtml.IHTMLControlRange)((mshtml.IHTMLBodyElement)HTMLDocument.body).createTextRange();
    rang.add((mshtml.IHTMLControlElement)img);
    rang.execCommand("Copy", false, null);
    bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
      

  7.   

    在项目中添加Microsoft.mshtml的引用
      

  8.   

    恩,代码看懂了一部分,这代码就是获得验证图片对不对?最后变成bmp格式保存
    那保存路径在哪里?
    没路径怎么放入Picturebox控件里面呢?
      

  9.   

    mshtml.IHTMLDocument2 HTMLDocument = (mshtml.IHTMLDocument2)this.axWebBrowser1.Document
    运行时提示:错误 1 无法将类型“System.Windows.Forms.HtmlDocument”转换为“mshtml.IHTMLDocument2” D:\Test\WindowsApplication1\WindowsApplication1\Form1.cs 109 50 WindowsApplication1
      

  10.   

    我的代码是用的com控件,你的是vs2005的webbrowser控件,稍微有点差异,
    你根据实际类型转换一下就可以了
      

  11.   

    恩,转换成功了.我稍微改了下.
    还是那个问题
    这代码就是获得验证图片对不对?最后变成bmp格式保存 
    那保存路径在哪里? 
    没路径怎么放入Picturebox控件里面呢?
    还有这句代码:mshtml.IHTMLImgElement img = (mshtml.IHTMLImgElement)HTMLDocument.all.item("Image1", null); 
    这个"Image1"指的是网页里那图片的名称吗?
    谢谢
      

  12.   

    Picturebox.Image = bmp;如果想保存可以用bmp.Save方法保存
      

  13.   

    试了,Picturebox控件里面没东西的
    问题出在哪了呢?
      

  14.   

    mshtml.IHTMLImgElement img = (mshtml.IHTMLImgElement)HTMLDocument.all.item("Image1", null);Image1是网页中img图片的id,你根据你自己的网页改一下可以单步调试一下看是哪没有取出数据
      

  15.   


    private Stream GetYZMpicture(string url,string cookie)  //url为取得验证码的网址/Cookie为取得验证码所需的cookie
            {
                long contentLength;
                HttpWebRequest httpWebRequest;
                HttpWebResponse webResponse;
                Stream stream;
                byte[] byteRequest ={ };            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);  //加载URL
                CookieContainer cookie_container = new CookieContainer();     //加载Cookie
                cookie_container.SetCookies(new Uri(url), cookie);
                httpWebRequest.CookieContainer = cookie_container;            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                httpWebRequest.Method = "Post";
                httpWebRequest.ContentLength = byteRequest.Length;  //设置Header            stream = httpWebRequest.GetRequestStream();
                stream.Write(byteRequest, 0, byteRequest.Length); //POST信息
                stream.Close();            webResponse = (HttpWebResponse)httpWebRequest.GetResponse(); //收到应答
                stream = webResponse.GetResponseStream();
                contentLength = webResponse.ContentLength;            byte[] outBytes = new byte[contentLength]; //转换为Byte
                outBytes = ReadFully(stream);
                stream.Close();            return (new MemoryStream(outBytes));        }        public static byte[] ReadFully(Stream stream)
            {
                byte[] buffer = new byte[128];
                using (MemoryStream ms = new MemoryStream())
                {
                    while (true)
                    {
                        int read = stream.Read(buffer, 0, buffer.Length);
                        if (read <= 0)
                            return ms.ToArray();
                        ms.Write(buffer, 0, read);
                    }
                }
            }调用Image img = new Bitmap(GetYZMpicture("http://... ..."));//获得验证码图片
                    pictureBox1.Image = img;
      

  16.   

    或请参考
    http://www.cnblogs.com/hobe/archive/2007/03/14/674292.html
    我就是通过改写了一下网站提供的代码,读取出了验证码图片.
      

  17.   

    调用 C# code
    Image img = new Bitmap(GetYZMpicture("http://... ..."));//获得验证码图片
                    pictureBox1.Image = img;
    调用的时候,GetYZMpicture("http://... ..."));
    只放入URL,怎么没有放入cookie?
    还有cookie只有在登陆过的时候才有的吧?
    网页上输入账号密码的时候就带这个验证码的。
    那没登陆过怎么有cookie值?
      

  18.   

    pupo大侠,有一点不好意思。那个网站的验证码图片没有ID,也没有name
      

  19.   


    呵呵,比如你打开填写帐号密码的网页时,网站会先给你一个cookie,然后给你页面的HTML,再根据给你的cookie去另一页面生成一个验证码,并生成一个图片显示给你.当你填写好后按提交键时,是按照一定格式返回个网站的,返回的串中就有cookie,这样网站根据cookie和验证码就知道你输入的是否合法.
    所以我用的都是直接与网站服务器的数据交换,而不用webBroser显示出内容,这样快的多,当然也复杂的多.
    如果你用webBroswer的话,还是请参考http://www.cnblogs.com/hobe/archive/2007/03/14/674292.html 
      

  20.   


    可以通过遍历所有img元素,根据src来判断是否是你需要的img
      

  21.   

    按你说的网站会先给我cookie,那么怎么获得这个cookie放入GetYZMpicture方法里呢?