HttpWebRequest图片采集
2019-04-15 17:17发布
生成海报
string url = "http:";
public Form1()
{
InitializeComponent();
this.Controls.OfType().ToList().ForEach(x => x.Click += pictureBox_Click);//给窗体的所有pictureBox注册pictureBox_Click事件
}
private void button1_Click(object sender, EventArgs e)
{
List < string> webHtml=new List ();
List webTitle = new List();
//string[] webTitle;
HttpWebRequest web = (HttpWebRequest)HttpWebRequest.Create(url);
web.Proxy = null;
HttpWebResponse weatherResponse = (HttpWebResponse)web.GetResponse();
//从Internet资源返回数据流
Stream webStream = weatherResponse.GetResponseStream();//
StreamReader reader = new StreamReader(webStream, Encoding.UTF8 );//获取当前系统的编码
string Html = reader.ReadToEnd();//读取流
webStream.Close();
reader.Close();//关闭流
textBox2.Text = Html;
string zhze = @"(?is).*?)'.*?.*?)alt"; //";
Regex pipei = new Regex(zhze,RegexOptions.Multiline);//(?is).*?)
if (pipei.IsMatch(Html))
{
MatchCollection strs = pipei.Matches (Html);
foreach (Match str in strs )
{
webHtml.Add(str.Groups["img"].Value);
webTitle.Add(str.Groups["title"].Value);
}
//Match str = pipei.Match(webHtml);
//webHtml = str.Groups["img"].Value ;
//webTitle = str.Groups["title"].Value;
//MessageBox.Show(webHtml);
}
else
{
MessageBox.Show("错误");
}
HttpWebRequest web1;
HttpWebResponse weatherResponse1;
Stream webStream1;
for (int i=1;i 6) return;
web1 = (HttpWebRequest)HttpWebRequest.Create("http://" + webHtml[i]);
//web1.Proxy = null;
weatherResponse1 = (HttpWebResponse)web1.GetResponse();
//从Internet资源返回数据流
webStream1 = weatherResponse1.GetResponseStream();//
PictureBox text =(PictureBox)this.Controls["PictureBox" + i.ToString()];
Label lb = (Label)this.Controls["Label" + i.ToString()];
text.Image = Image.FromStream(webStream1);
lb.Text = webTitle[i];
webStream1.Close();
}
}
图片测试,留个脚印
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮