Display asynchronous downloaded pictures in winform

  • 2021-09-24 23:31:18
  • OfStack


private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
  //// Utilization  WebClient  To download pictures 
  using (WebClient wc = new WebClient())
  {
    ////WebClient  Downloaded response event binding 
    wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wc_DownloadDataCompleted);

    //// Start asynchronous download, picture URL Please specify the path according to the actual situation 
    //// At the same time, DataGridView The line number of the current line is passed to specify the CELL
    wc.DownloadDataAsync(new Uri(this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()),
      e.RowIndex);
  }
}


void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
  //// If the download process has not occurred with errors and has not been cancelled halfway 
  if (e.Error == null && !e.Cancelled)
  {
    //// Displays the picture in the corresponding specified cell,  e.UserState  Is the incoming  e.RowIndex
    ////e.Result  Is the download result 
    this.dataGridView1.Rows[(int)e.UserState].Cells["src"].Value = e.Result;
    // this.dataGridView1.Rows[(int)e.UserState].Cells["test"].Value = GetImage("1");
  }
}

Related articles: