【问题标题】:Stream from IP cam C#从 IP cam C# 流式传输
【发布时间】:2014-12-19 02:06:00
【问题描述】:

我的以下代码不起作用。如果我加载到 Firefox 并从我的摄像头流式传输,我的 camUrl 链接有效,但在运行时我的图片框中没有显示任何内容。任何想法为什么?

        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;
            }
        }

【问题讨论】:

  • 您可以尝试在您的 while 循环中添加 image.Refresh()。我不确定这会奏效还是最好的方法,但至少你可能会看到一些东西。
  • 试过了,没有运气。

标签: c# stream ip picturebox


【解决方案1】:

看起来您从响应流中读取的循环不正确。您只能从响应中获得一个流,并且它上面会有多个图像。

您可能无法将响应流直接传递给 Image.FromStream - 图像可能以多部分响应编码,该响应使用文本分隔符分隔图像。您可以在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】:

camUrl 是否返回图像?

尝试调试sr = webRes.GetResponseStream(),如果不为null,尝试image.Invalidate()image.Update()

More information about invalidate, update and refresh

【讨论】:

  • 感谢您的回复。 sr = webRes.GetResponseStream() 不为空,我尝试了使图像无效和更新,但仍然没有任何显示。
  • 我无权访问网络摄像机,但如果您发布网络摄像机的临时链接,我将尝试调试您的代码
猜你喜欢
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
相关资源
最近更新 更多