我有以下代码:
Image tmpimg = null;
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
return Image.FromStream(stream);
在我输入Image.
的最后一行,FromStream
不在列表中。我该怎么办?
答案 0 :(得分:28)
更详细的使用示例和所需的命名空间。
using System.Net;
using System.IO;
using System.Drawing;
public static Image GetImageFromUrl(string url)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
using (Stream stream = httpWebReponse.GetResponseStream())
{
return Image.FromStream(stream);
}
}
}
希望这可以节省您一些时间,因为您可以快速复制并粘贴到您的解决方案中。
〜干杯!!
答案 1 :(得分:11)
试试这个:
using System.Drawing;
using System.IO;
using System.Net;
public static Image GetImageFromUrl(string url)
{
using (var webClient = new WebClient())
{
return ByteArrayToImage(webClient.DownloadData(url));
}
}
public static Image ByteArrayToImage(byte[] fileBytes)
{
using (var stream = new MemoryStream(fileBytes))
{
return Image.FromStream(stream);
}
}
答案 2 :(得分:8)
您可能需要using System.Drawing;
。
答案 3 :(得分:2)