//这里触发代码
using (WebClient wc = new WebClient())//防止内存泄漏
{
wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownloadDataCallback);
Uri uri = new Uri(“图片的下载地址”);
wc.DownloadDataAsync(uri);
}
//异步的事件
public void DownloadDataCallback(object sender, DownloadDataCompletedEventArgs e)
{
try
{
if (e.Result.Length > 0 && e.Error == null && e.Cancelled == false)
{
byte[] buffer = e.Result;
using (MemoryStream ms = new MemoryStream(buffer))
{
Bitmap bitmap = new Bitmap(ms);//得到数据
// pbMain.Image = bitmap;
}
}
}
catch
{
}
}
WebClient使用DownloadDataCompleted异步下载图片 比同步的好处就是 如果你的URL出现出错 同步会卡住程序 而异步不会 减少等待时间