下载网页中的图片到本地
简单的一个下载如下 :
string url = "http://avatar.csdn.net/A/2/6/2_yefengzhixia.jpg"; string filepath = "D:\\pic.jpg"; WebClient mywebclient = new WebClient(); mywebclient.DownloadFile(url, filepath); MessageBox.Show("OK");
下面演示一个从网站中下载所有图片到本地:
1、界面设计:
<Grid> <DockPanel> <GroupBox x:Name="groupBox" Header="图片网址" DockPanel.Dock="Top"> <StackPanel> <StackPanel Orientation="Horizontal" Margin="10" > <Button x:Name="button" Content="查看网页中的图片" Margin="5" Click="button_Click" /> <TextBlock Text="网址:"></TextBlock> <TextBox x:Name="textBox" Text="http://www.tooopen.com/img/87.aspx" HorizontalAlignment="Left" MaxWidth="600" TextWrapping="Wrap" MinWidth="300" VerticalAlignment="Top" /> </StackPanel> <TextBlock Text="下载信息:"></TextBlock> <TextBlock Margin="10" Name="txtcount" Text="{Binding downCount}"> </TextBlock> <TextBlock Name="total"></TextBlock> <Button Click="Button_Click_1" Content="打开下载图片文件夹"></Button> </StackPanel> </GroupBox> <ListBox Name="lb"></ListBox> </DockPanel> </Grid>
2、代码
public partial class MainWindow : Window,INotifyPropertyChanged { public MainWindow() { InitializeComponent(); this.DataContext = this; } private int c; public int downCount { get { return c; } set { c = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("downCount")); } } public ObservableCollection<string> list = new ObservableCollection<string>(); public event PropertyChangedEventHandler PropertyChanged; private async void button_Click(object sender, RoutedEventArgs e) { var path = textBox.Text; if (string.IsNullOrWhiteSpace(path)) { textBox.Focus(); MessageBox.Show("请输入网站地址"); return; } HttpClient client = new HttpClient(); var str = await client.GetStringAsync(new Uri(path)); Regex rg = new Regex("<img.+?src=\"(.+?)\""); var mth = rg.Matches(str); WebClient wc = null; int i = 0; total.Text="共有图片:"+mth.Count; Action<string> listboxAddImgName = x => { lb.Items.Add(x); }; foreach (Match item in mth) { i++; var img = item.Groups[1].Value; string Ex = img.Substring(img.LastIndexOf(".")); string imgName = i + Ex; Task.Run(() => { wc = new WebClient(); wc.DownloadFile(img, "DownLoadImages/" + imgName); return imgName; }).ContinueWith(r=> { downCount +=1; Dispatcher.Invoke(listboxAddImgName,r.Result); }); } } private void Button_Click_1(object sender, RoutedEventArgs e) { System.Diagnostics.Process.Start(Environment.CurrentDirectory+"/DownLoadImages"); } }
另一个方法,使用线程不完整,以后再完善:
HttpClient client = new HttpClient(); var http = await client.GetStringAsync(txturl.Text.Trim()); Regex rg = new Regex("<img.+?src=\"(.+?)\""); var ms = rg.Matches(http); List<Task> tasklist = new List<Task>(); count = ms.Count; foreach (Match m in ms) { if (m.Success) { string imgurl = m.Groups[1].ToString(); Task.Run(async () => { var downImgurl = System.IO.Path.Combine(path, imgurl.Substring(imgurl.LastIndexOf(@"/") + 1)); WebClient wc = new WebClient(); wc.DownloadFile(imgurl, downImgurl); //byte[] buf = await client.GetByteArrayAsync(imgurl); //File.WriteAllBytes(downImgurl, buf); lock (obj) { Dispatcher.Invoke(() => { list.Add(new Img() { url = downImgurl }); }); num++; } }); } }