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"); }}
以上就是顯示異步下載圖片的一些代碼片段,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答