I use this code to capture an image from IP Camera:
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();
}
But stream threw an exception because CanSeek
& CanWrite
is false.
I tried many ways, for example Copyto
(MemoryStream), but the problem still persists.
Would you please help me on that?
This is the code using MemoryStream:
Stream stm = resp.GetResponseStream();
MemoryStream ms = new MemoryStream();
stm.CopyTo(ms);
ms.Position = 0;
And this "ms" for ReadTimeout
& WriteTimeout
threw:
Message "Timeouts are not supported on this stream."
Because canTimeout()
is false for MemoryStream too.
Finally I found this solution, and it works well: https://stackoverflow.com/a/2368505/492628