C#实现循环发送电脑屏幕截图

这篇文章主要为大家详细介绍了C#实现循环发送电脑屏幕截图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#实现循环发送电脑屏幕截图的具体代码,供大家参考,具体内容如下

写的一个demo,建立Socket连接之后,循环发送电脑屏幕截图

服务器端开启之后监听端口号2000,为新建连接创建新的Socket。之后从客户端接收截图,判断一张图片接收结束之后。将图片显示于pictureBox控件上。


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.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
 
namespace server
{
    public partial class Form1 : Form
    {  
        public Thread mythread;
        public int shu = 0;
        public Form1()
        {
            InitializeComponent();
          //  CheckForIllegalCrossThreadCalls = false; 不需要   
        }
      
        public Socket nect(int port, string host)
        {
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket类    
            s.Bind(ipe);//绑定2000端口     
            s.Listen(0);//开始监听 
            Socket temp = s.Accept();//为新建连接创建新的Socket。 
            return temp;
        }
        public void look(object s)
        {
        Socket temp = (Socket)s;
        byte[] buffer = new byte[1024];
        while (true)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                // Thread.Sleep(1000);
                int end = 1;
                while (end != 0)
                {
                    shu = temp.Receive(buffer, buffer.Length, 0);//从客户端接受信息
                    if (shu == 5)
                        end = 0;
                    else
                        ms.Write(buffer, 0, 1024);
                }
                pictureBox1.Image = Bitmap.FromStream(ms);
                ms.Dispose();
            }
            catch(System.ArgumentException  e)
            {
                
            }
        } 
        }
        //开启
        private void button1_Click(object sender, EventArgs e)
        {
            int port = 2000;
            string host = "127.0.0.1";
            Socket temp = nect(port, host);
           
            mythread = new Thread(new ParameterizedThreadStart(look));
            mythread.Start(temp);
    
        }
 
    }
}

客户端连接到服务器后,获取屏幕截图之后,设置图片的大小和清晰度,并循环发送截图。


using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Drawing;
using System.Windows.Forms;
using System.CodeDom;
using System.Drawing.Imaging;
using System.Drawing.Design;
using System.Text;
using System.Threading;
 
 
namespace client
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 2000; 
            string host = "127.0.0.1";
            Socket c=connect(port, host);
 
            Bitmap bt = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics g= Graphics.FromImage(bt);
            while(true)//循环发送截图
            {
 
            MemoryStream ms = new MemoryStream();
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.PrimaryScreen.Bounds.Size);//获取屏幕截图
            Image mm = SaveJpg(bt,10);//设置图片清晰度
            mm = GetWebImage(mm,360,240);//改变截屏图片大小
            mm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
 
            byte[] buffer = new byte[1024];
            ms.Position = 0;  
          
            int end = 1;
            while (end != 0)
            {
                end= ms.Read(buffer, 0, 1024);//end为零表示读取完毕
                c.Send(buffer, buffer.Length, 0);//每次发送1024个字节
            }
            string sendStr = "over!";//结束信息
            byte[] bs = Encoding.ASCII.GetBytes(sendStr); 
            c.Send(bs, bs.Length, 0);//发送测试信息
            ms.Dispose();
            }
            c.Close();  
            Console.ReadLine();
        }
        public static Socket connect(int port, string host)
        {
            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例 
            Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket    
            Console.WriteLine("Conneting...");
            try
            {
               
                c.Connect(ipe);//连接到服务器 
                return c;
            }
            catch
            {
                Thread.Sleep(1000);
                connect(port, host);
                return c;
            
            }
 
        }
        public static Image SaveJpg(Image image, long value)//设置图像质量1—100
        {
            ImageCodecInfo icInfo = null;
            ImageCodecInfo[] infos = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo info in infos)
            {
                if (info.MimeType == "image/jpeg")
                {
                    icInfo = info;
                    break;
                }
            }
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, value);//质量,定义图片的清晰度
            ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);//压缩,似乎无效果
            return image;
        }
        public static Image GetWebImage(Image img, int p_Width, int p_Height)//改变图片大小
        {
            Bitmap _Bitmap = new Bitmap(p_Width, p_Height);
            Graphics _Graphcis = Graphics.FromImage(_Bitmap);
            _Graphcis.DrawImage(img, 0, 0, p_Width, p_Height);
            _Graphcis.Dispose();
            return _Bitmap;
        }
 
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。

本文标题为:C#实现循环发送电脑屏幕截图

基础教程推荐