我是c#的新手,我为流视频编写代码我不知道这是否正确
听众
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Socket sk;
private NetworkStream ns;
private TcpListener tlp;
private Thread th;
void res()
{
try
{
tlp = new TcpListener(2100);
tlp.Start();
sk = tlp.AcceptSocket();
ns = new NetworkStream(sk);
pictureBox1.Image = Image.FromStream(ns);
tlp.Stop();
if (sk.Connected == true)
{
while (true)
{
res();
}
}
}catch(Exception ex){}
}
private void Form1_Load(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(res));
th.Start();
}
}
}
和客户
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection capture;
private VideoCaptureDevice frame;
private NetworkStream ns;
private TcpClient tlp;
private MemoryStream ms;
private BinaryWriter br;
private void Form1_Load(object sender, EventArgs e)
{
capture = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach(FilterInfo de in capture){
com.Items.Add(de.Name);
}
//com.SelectedIndex = 0;
}
private void st_Click(object sender, EventArgs e)
{
frame = new VideoCaptureDevice(capture[com.SelectedIndex].MonikerString);
frame.NewFrame += frame_NewFrame;
frame.Start();
}
void frame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
try
{
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Bmp);
byte[] buffer = ms.GetBuffer();
ms.Close();
tlp = new TcpClient("192.168.0.104", 2100);
ns = tlp.GetStream();
br = new BinaryWriter(ns);
br.Write(buffer);
tlp.Close();
ms.Close();
ns.Close();
br.Close();
}
catch (Exception ex) { MessageBox.Show(ex.Message); };
}
}
}
我在同一网络上的两台计算机上进行测试 它显示来自另一台计算机的图像,但它如此缓慢 我的代码中的问题是什么
答案 0 :(得分:0)
您在客户端的表单线程中执行所有操作,而不是在后台异步缓冲数据,只在主线程上播放它。还有其他多种方法可以执行您想要执行的操作,其中一种方法是在窗体中嵌入Windows Media Player控件以播放WMP支持的任何可流式格式。
如果您要做的只是流数据,那么在客户端的NewFrame处理程序中,您应该做的唯一事情是缓冲缓冲区中的数据,可能是Queue<Bitmap>
或您的数据所处的格式。您应该跟踪主线程上缓冲的帧数,并在缓冲区中至少有2-3帧后启动显示(如果它们是视频,则为5-10秒)。您的显示例程将从缓冲区中取消()数据,直到为空。然后,您再次等待更多数据被缓冲。请注意,您需要使用锁定对象来防止读写线程之间的冲突,或者只使用.NET 4.0中引入的新并发集合之一,如ConcurrentQueue<T>
(在System.Collections.Concurrent中)。 (https://msdn.microsoft.com/en-us/library/dd267265%28v=vs.100%29.aspx)