Stream from IP cam C#

2019-04-12 11:52发布

问题:

I have the following code which isn't working. My camUrl link works if I load into Firefox and streams from my cam, but nothing is displayed in my picturebox at runtime. Any ideas why?

        public Thread _camThread;
        private string camUrl = "http://my-domain-ip:2080/videostream.cgi?user=admin&pwd=password";
        public HttpWebRequest webReq;
        public WebResponse webRes;
        public Stream sr;

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (_camThread == null) _camThread = new Thread(new ThreadStart(RunCam));
            _camThread.Start();
        }

        private void RunCam()
        {
            try
            {
                webReq = (HttpWebRequest)WebRequest.Create(camUrl);
                webReq.AllowWriteStreamBuffering = true;
                webReq.Timeout = 20000;
                using (webRes = webReq.GetResponse())
                {
                    while ((sr = webRes.GetResponseStream()) != null)
                    {
                        image.Image = Image.FromStream(sr);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (_camThread.IsAlive)
            {
                _camThread.Abort();
                _camThread = null;
            }
        }

回答1:

It looks like your loop for reading from the response stream is incorrect. You only ever get one stream from a response, and it will have multiple images on it.

It is likely that you can't pass the response stream to Image.FromStream directly - the images are probably encoded in a multi-part response that separates the images with textual delimiters. You can learn more about the format of multi-part responses at RFC2046.

using (webRes = webReq.GetResponse())
{
    using (sr = webRes.GetResponseStream())
    {
        // continuously read images from the response stream until error
        while (true)
        {
            try
            {
                // note: the line below probably won't work, you may need to parse
                // the next image from the multi-part response stream manually
                image.Image = Image.FromStream(sr);


                // if the above doesn't work, then do something like this:
                // var imageBytes = ParseNextImage(sr);
                // var memoryStream = new MemoryStream(imageBytes);
                // image.Image = Image.FromStream(memoryStream);
            }
            catch(Exception e)
            {
                Console.WriteLine("Aborting read from response stream due to error {0}", e);
                break;
            }
        }
    }
}


回答2:

Does the camUrl return an image?

Try to debug sr = webRes.GetResponseStream() and if it is not null, try image.Invalidate() or image.Update()

More information about invalidate, update and refresh