欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

winform 中显示异步下载的图片

时间:2021-05-27 08:41:40|栏目:.NET代码|点击:
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");
  }
}

上一篇:c# 遍历 Dictionary的四种方式

栏    目:.NET代码

下一篇:js获取Treeview选中的节点(C#选中CheckBox项)

本文标题:winform 中显示异步下载的图片

本文地址:http://www.codeinn.net/misctech/129524.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有