4

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

Community
  • 1
  • 1
Negar
  • 866
  • 2
  • 10
  • 20

1 Answers1

2

You should be able to copy the stream into a memory stream if it isn't seekable

Here's a post that might help.

Community
  • 1
  • 1
Jofairden
  • 95
  • 9