Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当Stream.CanSeek为false时,我为什么要这样做_C#_Stream - Fatal编程技术网

C# 当Stream.CanSeek为false时,我为什么要这样做

C# 当Stream.CanSeek为false时,我为什么要这样做,c#,stream,C#,Stream,我使用以下代码从IP摄像头捕获图像: HttpWebRequest reqs = (HttpWebRequest)WebRequest.Create("http://" + ip + snapshotCommand); reqs.Method = "POST"; reqs.Timeout = 4000; reqs.Credentials = new NetworkCredential(user, pass); reqs.PreAuthenticate = true; HttpWebRespon

我使用以下代码从IP摄像头捕获图像:

HttpWebRequest reqs = (HttpWebRequest)WebRequest.Create("http://" + ip + snapshotCommand);
reqs.Method = "POST";
reqs.Timeout = 4000;
reqs.Credentials = new NetworkCredential(user, pass);
reqs.PreAuthenticate = true;

HttpWebResponse resp = (HttpWebResponse)reqs.GetResponse();
if (resp != null)
{
    Stream stm = resp.GetResponseStream();
    img = new Bitmap(stm);
    stm.Close();
}
但流引发了异常,因为
CanSeek
&
CanWrite
为false。 我尝试了很多方法,例如
Copyto
(MemoryStream),但问题仍然存在。 你能帮我吗

这是使用MemoryStream的代码:

Stream stm = resp.GetResponseStream(); 
MemoryStream ms = new MemoryStream(); 
stm.CopyTo(ms); 
ms.Position = 0; 
ReadTimeout
WriteTimeout
的这个“ms”抛出: 消息“此流不支持超时。” 因为
canTimeout()
对于MemoryStream也是false

最后我找到了这个解决方案,而且效果很好:

如果无法查找,您应该能够将该流复制到内存流中


这可能会有所帮助。

我还尝试了缓冲区解决方案,显示复制到
内存流的代码以及您遇到的错误不将代码粘贴到注释中;这样的代码是不可读的。请编辑问题并将代码粘贴到那里。我已将代码添加到问题中。此解决方案也引发了异常。问题可能是Web服务提供商的供应商方面。我在浏览器中使用snapshot命令轻松捕获
图像
,但在代码中读取流时,存在这些问题