将动态生成的图像转换为 win 形式

本文关键字:转换 win 形式 图像 动态 | 更新日期: 2023-09-27 18:32:49

我正在尝试通过HttpWebRequest获取图像并以获胜形式显示它imagebox。通过 Fiddler ImageView 选项卡跟踪请求时,我可以看到可以正确查看图像,但在读取流时Stream was not readable我收到错误

Image img = Image.FromStream(stream). 

我错过了什么?

HttpWebRequest req =  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("[URL here]");
HttpWebResponse response = (HttpWebResponse)req.GetResponse();    
Stream stream = response.GetResponseStream();
Image img = Image.FromStream(stream);   // ERROR occurs here
stream.Close();

将动态生成的图像转换为 win 形式

经过一番挖掘,在这里找到了答案 C# gif 图像到内存流并返回(丢失动画):

HttpWebRequest req =  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("[URL here]");
HttpWebResponse response = (HttpWebResponse)req.GetResponse();    
Stream stream = response.GetResponseStream();
MemoryStream memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
memoryStream.Position = 0;
stream = memoryStream;
Image img = Image.FromStream(stream);   
stream.Close();