最好的方式图像网址到Drawing.Image [英] Best way image url to Drawing.Image
本文介绍了最好的方式图像网址到Drawing.Image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将图片从网址加载到Drawing.Image的最佳方式?
Best way to load an image from url to Drawing.Image?
我发现了这两种方式,哪种方式更好还是更好?
I found this two ways, which one is better or is there any better?
PictureBox pb = new PictureBox();
pb.ImageLocation = "http://lovelypackage.com/wp-content/uploads/2012/02/lovely-package-whatever-wine4.jpg";
Image img = pb.Image;
OR
using (WebClient wc = new WebClient())
{
byte[] bytes = wc.DownloadData("http://lovelypackage.com/wp-content/uploads/2012/02/lovely-package-whatever-wine4.jpg");
MemoryStream ms = new MemoryStream(bytes);
Image img = Image.FromStream(ms);
}
推荐答案
如果你不使用在您的应用程序中的PictureBox,我建议第二种方法。我更进一步,使它成为一个方法,采取 url
字符串并返回图像
使其可重复使用别处。此外,您可能希望使用 DownloadDataAsync
来避免阻止UI线程,特别是如果图像可能需要花费相当多的时间来下载。
If you're not using the PictureBox in your application, I'd recommend the second approach. I'd go one step further and make it a method that takes a url
string and returns the Image
making it reusable elsewhere. Also, you might want to use DownloadDataAsync
to avoid blocking the UI thread, especially if the image may take a noticeable amount of time to download.
这篇关于最好的方式图像网址到Drawing.Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文