winform 中显示异步下载的图片
发布时间:2020-12-15 06:38:39 所属栏目:百科 来源:网络整理
导读:private void dataGridView1_RowsAdded(object sender,DataGridViewRowsAddedEventArgs e){ ////利用 WebClient 来下载图片 using (WebClient wc = new WebClient()) { ////WebClient 下载完毕的响应事件绑定 wc.DownloadDataCompleted += new DownloadDataC
private void dataGridView1_RowsAdded(object sender,DataGridViewRowsAddedEventArgs e) { ////利用 WebClient 来下载图片 using (WebClient wc = new WebClient()) { ////WebClient 下载完毕的响应事件绑定 wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted); ////开始异步下载,图片URL路径请根据实际情况自己去指定 ////同时将DataGridView当前行的行号传递过去,用于指定图片显示的CELL wc.DownloadDataAsync(new Uri(this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()),e.RowIndex); } } void wc_DownloadDataCompleted(object sender,DownloadDataCompletedEventArgs e) { ////如果下载过程未发生错误,并且未被中途取消 if (e.Error == null && !e.Cancelled) { ////将图片显示于对应的指定单元格, e.UserState 就是传入的 e.RowIndex ////e.Result 就是下载结果 this.dataGridView1.Rows[(int)e.UserState].Cells["src"].Value = e.Result; // this.dataGridView1.Rows[(int)e.UserState].Cells["test"].Value = GetImage("1"); } } 以上就是显示异步下载图片的一些代码片段,希望能给大家一个参考,也希望大家多多支持编程小技巧。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |