方法一:
private void button1_Click(object sender, RoutedEventArgs e)
{
WebClient webClientImgDownloader = new WebClient();
webClientImgDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(webClientImgDownloader_OpenReadCompleted);
webClientImgDownloader.OpenReadAsync(new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute));
}
void webClientImgDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.Result); // ERROR HERE!
image1.Source = bitmap;
}
方法二:
//将本地资源文件到Image控件上,图片的Bulid Action属性要设置成Content
Uri uri = new Uri("/Images/meinv.jpg", UriKind.Relative);
//直接将网络图片显示到Image控件上
Uri uri2 = new Uri("http://www2.ff369.com/tupian/201103/010/1.jpg",UriKind.Absolute);
ImageSource imgSource = new BitmapImage(uri);
image1.Source = imgSource;
//当资源图片资源文件的属性是Resource时,使用下面的方法将图片呈现到Image控件上,但是不建议使用这种方法
//在网上查看资料说将图片以Resource会造成闪屏
Stream stream = App.GetResourceStream(new Uri("/DemoCode2;component/Images/meinv.jpg", UriKind.Relative)).Stream;
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(stream ); // ERROR HERE!
image1.Source = bitmap;