gpt4 book ai didi

c# - 从 IP cam C# 流式传输

转载 作者:行者123 更新时间:2023-11-30 14:09:54 31 4
gpt4 key购买 nike

我的以下代码无法正常工作。如果我从我的摄像头加载到 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;
}
}

最佳答案

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

您可能无法将响应流直接传递给 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;
}
}
}
}

关于c# - 从 IP cam C# 流式传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26527520/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com