C# 通过UDP 远程监控摄像头

背景: 项目需要到网上想下载一个UDP远程通信的例子,但是居然没有下到合适的例子。

资源下载:https://download.csdn.net/download/gigizhongyan/12319275

 

实现效果

C# 通过UDP 远程监控摄像头_第1张图片

 

 

关键代码:

 

客户端 采集摄像头数据 通过UDP发送

 

   private void CameraConn()

        {

            videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);

 

            videoSourcePlayer1.VideoSource = videoSource;

            videoSourcePlayer1.NewFrame += videoPlayer_RGB_NewFrame;

            videoSourcePlayer1.Start();

 

        }

 

 void videoPlayer_RGB_NewFrame(object sender, ref Bitmap image)

        {

            var ip = IPAddress.Parse(server.Text);

            var multicast = new IPEndPoint(ip, 4444);

            MemoryStream ms = new MemoryStream();

            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

            byte[] bytes = new byte[ms.Length];

            bytes = ms.ToArray();

 

 

            client.Send(bytes, bytes.Length, multicast);

            //Thread.Sleep(1);

 

        }

 

服务端代码:

 

 private void Form1_Load(object sender, EventArgs e)

        {

            //创建等待连接线程

            Thread thread = new Thread(new ThreadStart(waitConnect));

            thread.IsBackground = true;

            thread.Start();

        }

   private void waitConnect()

        {

        

            client = new UdpClient(new IPEndPoint(IPAddress.Any, UDP_PORT));

             

         

 

            //启动接收线程

            Thread thread = new Thread(new ThreadStart(recvImage));

            thread.IsBackground = true;

            thread.Start();

        }

 

  private void recvImage()

        {

            while(true)

            {

                //接受图像帧数据

                byte[] recvBuf = client.Receive(ref remote);

                MemoryStream ms = new MemoryStream(recvBuf);

                try

                {

                    //显示到pictureBox上

                    pictureBox1.Image = Image.FromStream(ms);

                }

                catch (ArgumentException)

                { }

            }

        }

 

你可能感兴趣的:(C#)