用Webbrowser控件打开一个网页后,如何用Picturebox读取网页里的图片啊?
我想用Picturebox显示网页中的验证码,但是不知道怎么处理。
比如说有个Webbrowser 控件名字是:Webbrowser1
Picturebox控件名字是:Picturebox1
希望高手给小弟直接代码,小弟不是很懂的.
所以恳求高手能多给予代码注解.谢谢
谢谢
我想用Picturebox显示网页中的验证码,但是不知道怎么处理。
比如说有个Webbrowser 控件名字是:Webbrowser1
Picturebox控件名字是:Picturebox1
希望高手给小弟直接代码,小弟不是很懂的.
所以恳求高手能多给予代码注解.谢谢
谢谢
http://ptlogin2.qq.com/getimage?aid=11000101&'+ Math.random();
最后是Math.random
没办法获取的地址的
有其它方法吗?
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
Dim HTMLDocument As IHTMLDocument2 = CType(AxWebBrowser1.Document, IHTMLDocument2)
Dim bmp As Bitmap
这种格式什么含义啊?
定义bmp为Bitmap?
c#中怎么说啊..不知道了
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);
那保存路径在哪里?
没路径怎么放入Picturebox控件里面呢?
运行时提示:错误 1 无法将类型“System.Windows.Forms.HtmlDocument”转换为“mshtml.IHTMLDocument2” D:\Test\WindowsApplication1\WindowsApplication1\Form1.cs 109 50 WindowsApplication1
你根据实际类型转换一下就可以了
还是那个问题
这代码就是获得验证图片对不对?最后变成bmp格式保存
那保存路径在哪里?
没路径怎么放入Picturebox控件里面呢?
还有这句代码:mshtml.IHTMLImgElement img = (mshtml.IHTMLImgElement)HTMLDocument.all.item("Image1", null);
这个"Image1"指的是网页里那图片的名称吗?
谢谢
问题出在哪了呢?
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;
http://www.cnblogs.com/hobe/archive/2007/03/14/674292.html
我就是通过改写了一下网站提供的代码,读取出了验证码图片.
Image img = new Bitmap(GetYZMpicture("http://... ..."));//获得验证码图片
pictureBox1.Image = img;
调用的时候,GetYZMpicture("http://... ..."));
只放入URL,怎么没有放入cookie?
还有cookie只有在登陆过的时候才有的吧?
网页上输入账号密码的时候就带这个验证码的。
那没登陆过怎么有cookie值?
呵呵,比如你打开填写帐号密码的网页时,网站会先给你一个cookie,然后给你页面的HTML,再根据给你的cookie去另一页面生成一个验证码,并生成一个图片显示给你.当你填写好后按提交键时,是按照一定格式返回个网站的,返回的串中就有cookie,这样网站根据cookie和验证码就知道你输入的是否合法.
所以我用的都是直接与网站服务器的数据交换,而不用webBroser显示出内容,这样快的多,当然也复杂的多.
如果你用webBroswer的话,还是请参考http://www.cnblogs.com/hobe/archive/2007/03/14/674292.html
可以通过遍历所有img元素,根据src来判断是否是你需要的img