从IP凸轮C#流 [英] Stream from IP cam C#
问题描述
我有下面的代码,这是行不通的。我的 camUrl
链接的作品,如果我加载到Firefox和从我的凸轮流,但没有显示在我的在运行时的PictureBox。任何想法,为什么
公共主题_camThread?;
私人字符串camUrl =HTTP://我的域-IP:2080 / videostream.cgi用户名= Admin&安培; PWD =密码;
公众HttpWebRequest的webReq;
公共WebResponse的处Webres;
公共数据流Sr;
私人无效btnStart_Click(对象发件人,EventArgs五)
{
如果(_camThread == NULL)_camThread =新主题(新的ThreadStart(RunCam));
_camThread.Start();
}
私人无效RunCam()
{
试
{
webReq =(HttpWebRequest的)WebRequest.Create(camUrl);
webReq.AllowWriteStreamBuffering = TRUE;
webReq.Timeout = 20000;
使用(处Webres = webReq.GetResponse())
{
,而((SR = webRes.GetResponseStream())!= NULL)
{
image.Image = Image.FromStream(SR);
}
}
}
赶上(异常前)
{
MessageBox.Show(ex.Message);
}
}
私人无效btnStop_Click(对象发件人,EventArgs五)
{
如果(_camThread.IsAlive)
{
_camThread.Abort();
_camThread = NULL;
}
}
据看起来像你从响应流中读取循环不正确。你永远只从一个响应一个流,并且将在其上有多个图像。
有可能是你无法通过响应流直接Image.FromStream - 图像在分隔文本与图像的多部分反应可能编码分隔符。您可以在 RFC2046 了解的多部分响应的格式。
使用(处Webres = webReq.GetResponse())使用
{
(SR = webRes.GetResponseStream())
{
//连续朗读,直到错误
,而(真)
{
试
{
//注意从响应流中的图像:下面可能行都不行,你可能需要解析
//由多部分响应流中的下一个图像手动
image.Image = Image.FromStream(SR);
//如果上述不工作,然后做这样的事情:
// VAR imageBytes = ParseNextImage(SR);
// VAR的MemoryStream =新的MemoryStream(imageBytes);
// image.Image = Image.FromStream(MemoryStream的);
}
赶上(例外五)
{(,E
Console.WriteLine正在中止从响应流中,由于错误{0}读);
中断;
}
}
}
}
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;
}
}
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;
}
}
}
}
这篇关于从IP凸轮C#流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!