picturebox加载图片的三种方法与网站验证码的抓取
第一种:(此方法比较笨)
在页面上隐藏几个需要改变页面上图片的picturebox,比如下面的picFrom
在需要改变图片的方法处先定义:
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
然后就可以改变了(比如picTo的图片要改变成picFrom的图片)
this.picTo.Image = ((System.Drawing.Image)(resources.GetObject("picFrom.Image")));
第二种:
使用 FileStream 对象,如下所示:
Dim fs As System.IO.FileStream ' Specify a valid picture file path on your computer. fs = New System.IO.FileStream("C:/WINNT/Web/Wallpaper/Fly Away.jpg", IO.FileMode.Open, IO.FileAccess.Read) PictureBox1.Image = System.Drawing.Image.FromStream(fs) fs.Close()
第三种(我认为是比较好的)
使用 Image.FromFile 方法在 PictureBox 控件中加载图片,该图片文件将在您启动应用程序时锁定。
在应用程序运行时,图片文件保持锁定。 即使在运行时将 Image 属性设置为 Nothing,图片文件仍将锁定。
PictureBox1.Image = Image.FromFile("C:/WINNT/Web/Wallpaper/Fly Away.jpg")
//默认页面请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(DefaultUrl);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0.1) Gecko/20100101 Firefox/5.0.1";
request.Accept = "image/webp,*/*;q=0.8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
string WebContent = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
//验证码请求
request = (HttpWebRequest)WebRequest.Create(CodeUrl);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0.1) Gecko/20100101 Firefox/5.0.1";
request.Accept = "image/webp,*/*;q=0.8";
request.CookieContainer = lCC;//!Very Important.!!!
response = (HttpWebResponse)request.GetResponse();
response.Cookies = lCC.GetCookies(request.RequestUri);
stream = response.GetResponseStream();
picCode.Image = Image.FromStream(stream);
//stream.Dispose();
以上就是本文的全部内容了,希望大家能够喜欢。
上一篇:C#中使用HttpDownLoadHelper下载文件实例
栏 目:.NET代码
下一篇:.NET+JS对用户输入内容进行字数提示功能的实例代码
本文标题:picturebox加载图片的三种方法与网站验证码的抓取
本文地址:http://www.codeinn.net/misctech/190697.html