C#GDI+视屏广播接收端

来源:互联网 发布:淘宝助理发货网点设置 编辑:程序博客网 时间:2024/05/16 08:41
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Drawing.Imaging;
using System.Net;
using System.Net.Sockets;


namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            Form.CheckForIllegalCrossThreadCalls = false;//这里是为了让我们跨线程访问空间的时候不会报错
        }


        private void Form2_Load(object sender, EventArgs e)
        {
            //接收广播***********
            //新建一个线程来接收广播
            Thread th = new Thread(new ThreadStart(RecviceData));
            th.IsBackground = true;
            th.Start();


        }
        int port = 2000;//端口
        public void RecviceData()
        {
            //要接收的ip
            IPAddress ip = IPAddress.Any;
            //创建接收对象
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            //绑定接收对象
            EndPoint iep=new IPEndPoint(ip,port);
            server.Bind(iep);
            //准备接受的容器
          byte[] buffer=new byte[600000];
            //int width=Screen.PrimaryScreen.Bounds.Width;
            //int height=Screen.PrimaryScreen.Bounds.Height;
          while (true)//循环接收
          {
              //Bitmap bmp = new Bitmap(width, height);             
              
              MemoryStream ms = new MemoryStream();
              int len = server.Receive(buffer);
              ms.Write(buffer, 0, len);
              while (true)//
              {
                  if (len==1&&buffer[0]==100)//如果收到对方发送的标识(只有一个字节,并且第一个字节为100)则跳出循环
                  {
                      break;   
                  }
                   len = server.Receive(buffer);
                  ms.Write(buffer, 0, len);
              }
            Bitmap bmp=new Bitmap(ms);//将流转化为图片
            pictureBox1.Image = bmp;//将图片显示在pictureBox上
            pictureBox1.Refresh();//刷新控件
          }
        }


        private void 全屏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;//设置窗体的边框样式为无边框
            this.WindowState = FormWindowState.Maximized;//默认窗体的显示尺寸为最大
            this.TopMost = true;//设置窗体默认显示在最前面
            
            
        }


        private void 窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.Sizable;
            this.WindowState = FormWindowState.Normal;
            
            
        }


        private void 居中ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
        }


        private void 平铺ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
        }


        private void 拉伸ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        }
    }

}


原理:

将从网络上接受到的字节流转化为图片,并且显示在picturebox控件上,利用线程不停的接受图片并且显示在控件上,就可以实现动态效果。

缺点:

因为是从桌面上截的图,所以在广播中是看不到光标和鼠标的,而且图片的传输很耗费网络资源,整个程序的运行效果可能不是很流畅。

原创粉丝点击