WebClient的使用说明
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Net; using System.Windows.Forms; namespace WebClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnShow_Click(object sender, EventArgs e) { Uri uri = new Uri(this.txtUrl.Text, UriKind.Absolute); System.Net.WebClient webClient = new System.Net.WebClient(); //方法一 异步 //webClient.OpenReadAsync(uri); //加载完成之后显示图片 //webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); //webClient.OpenReadCompleted += (o, args) => //{ // Stream stream = args.Result; // Image img = new Bitmap(stream); // this.picBox.Image = img; //}; //方法二 var response = webClient.OpenRead(uri); if (response != null) this.picBox.Image=new Bitmap(response); //其它方法自行测试... } void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { Stream stream = e.Result; Image img = new Bitmap(stream); this.picBox.Image = img; } } }
欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如果感觉对您有用,请点击推荐。您的支持,是我的动力!