C# 无法在 Image 类中找到 FromStream
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10077219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to locate FromStream in Image class
提问by user896692
I have the following code:
我有以下代码:
Image tmpimg = null;
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
return Image.FromStream(stream);
On the last line when I type in Image., FromStreamisn't in the list. What can I do?
在我输入的最后一行Image.,FromStream不在列表中。我能做什么?
采纳答案by SLaks
You probably need using System.Drawing;.
你可能需要using System.Drawing;.
回答by Rogala
More detailed out example with using and the namespaces needed.
使用更详细的示例和所需的命名空间。
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);
}
}
}
Hopefully this saves you some time, since you can just do a quick copy and paste into your solution.
希望这可以为您节省一些时间,因为您可以快速复制并粘贴到您的解决方案中。
~Cheers!!
~干杯!!
回答by Sergey Malyutin
try this one:
试试这个:
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);
}
}
回答by lznt
btw, you also need to add reference to System.Drawing.dll, only adding using System.Drawing is not enough.
btw,您还需要添加对 System.Drawing.dll 的引用,仅添加 using System.Drawing 是不够的。

