Iam new in c# and i write code for stream video i dont know if this the Right way
the Listener
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();
}
}
}
and the client
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); };
}
}
}
and i test in on tow computer on same network its show image from the other computer but its so so slowly what the problem in my code
You do everything in the form thread in your client, instead you should buffer the data asynchronously in the background and only play it on the main thread. There are also multiple other ways to do what you want to do, one of them is to embed the Windows Media Player control in your form to play any streamable format WMP supports.
If all you want to do is stream data then in your client's NewFrame handler the only thing you should be doing is buffering the data in a buffer, probably a Queue<Bitmap>
or whatever format your data is in. You should keep track of how many frames are buffered on your main thread and initiate display after you have at least 2-3 frames in buffer (or 5-10 seconds if they are videos). Your display routines will .Dequeue() data from your buffer until empty. Then you again wait for more data to be buffered. Note that you will need to use a lock object to prevent collisions between your reading and writing thread, or simply use one of the new concurrent collections introduced in .NET 4.0 like the ConcurrentQueue<T>
(in System.Collections.Concurrent). (https://msdn.microsoft.com/en-us/library/dd267265%28v=vs.100%29.aspx)