使用AForge获取视频,在传输时遇到问题
两个程序,一个发送,一个接收
发送端:
NetworkStream ns;
MemoryStream ms;
int count;
void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
ms = new MemoryStream();
bmp = (Bitmap)eventArgs.Frame.Clone();
Bitmap bmpClone = (Bitmap)eventArgs.Frame.Clone();
bmpClone.Save(ms, ImageFormat.Bmp);
byte[] by = new byte[1024];
while (0 != (count = ms.Read(by, 0, 1024)))
{
ns.Write(by, 0, count);
}
ms.Dispose();
}
使用的TCP传输,ns是绑定到已经连接的TCPClient。这里简写了
/////////////////////////////////////////////////////////////////////////////////////////////////
接收端:
TcpClient tcp;
TcpClient remotetcp;
TcpListener tcplistener;
NetworkStream ns;
MemoryStream ms;
int count;
//FilterInfoCollection filterInfoCollection;
//VideoCaptureDevice video;
//Bitmap bmp;
public Form1()
{
InitializeComponent();
//filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
//video = new VideoCaptureDevice(filterInfoCollection[0].MonikerString);
//video.NewFrame += FinalVideo_NewFrame;
}
private void button1_Click(object sender, EventArgs e)
{
tcplistener = new TcpListener(11111);
tcplistener.Start();
tcp = tcplistener.AcceptTcpClient();
ns = tcp.GetStream();
//video.Start();
Thread th = new Thread(new ThreadStart(receive));
th.IsBackground = true;
th.Start();
}
public void receive()
{
while (true)
{
ms = new MemoryStream();
byte[] by = new byte[1024];
MessageBox.Show("11");
while (0 != (count = ns.Read(by, 0, 1024)))
{
ms.Write(by, 0, count);
}
ms.Position = 0;
bmp = new Bitmap(ms);
pictureBox1.Image = bmp;
ms.Dispose();
bmp.Dispose();
}
}
感觉写的很乱 逻辑也很乱,那位大神给个思路或者帮忙改下,本人小白,代码很垃圾。 --------------------编程问答-------------------- FinalVideo_NewFrame是一个NewFrameEventHandler事件,应该是个无线循环 --------------------编程问答-------------------- 刚通过测试发现接收端并没有生成bmp图片,原因可能是ns.Read(by, 0, 1024))一直在读,发送端在不停的发送
networkstream,而接收端也在不停的读,直到没内存了.....大神帮忙看下是不是这样,我是这样猜的,
如果这样,那要怎么解决呢?Application.DoEvents()?
另外我电脑刚刚出现问题了.....所有程序滚动条都只剩一半了。。。。。。怎么回事啊....
补充:.NET技术 , C#