winform 中顯示異步下載的圖片。本站提示廣大學習愛好者:(winform 中顯示異步下載的圖片)文章只能為提供參考,不一定能成為您想要的結果。以下是winform 中顯示異步下載的圖片正文
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");
}
}
以上就是顯示異步下載圖片的一些代碼片斷,願望能給年夜家一個參考,也願望年夜家多多支撐。