SoFunction
Updated on 2025-03-01

C# realizes looping sending computer screenshots

This article shares the specific code for C# to implement circular sending computer screenshots for your reference. The specific content is as follows

A demo written, after establishing a Socket connection, sending a computer screenshot loop

After the server is enabled, listen for port number 2000 and create a new socket for the new connection. Then receive screenshots from the client and determine that after receiving an image is completed. Display the image on the pictureBox control.

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
 
namespace server
{
    public partial class Form1 : Form
    {  
        public Thread mythread;
        public int shu = 0;
        public Form1()
        {
            InitializeComponent();
          // CheckForIllegalCrossThreadCalls = false; not required        }
      
        public Socket nect(int port, string host)
        {
            IPAddress ip = (host);
            IPEndPoint ipe = new IPEndPoint(ip, port);
            Socket s = new Socket(, , );//Create a Socket class            (ipe);//Bind port 2000            (0);//Start monitoring            Socket temp = ();//Create a new socket for a new connection.            return temp;
        }
        public void look(object s)
        {
        Socket temp = (Socket)s;
        byte[] buffer = new byte[1024];
        while (true)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                // (1000);
                int end = 1;
                while (end != 0)
                {
                    shu = (buffer, , 0);//Receive information from the client                    if (shu == 5)
                        end = 0;
                    else
                        (buffer, 0, 1024);
                }
                 = (ms);
                ();
            }
            catch(  e)
            {
                
            }
        } 
        }
        //Open        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));
            (temp);
    
        }
 
    }
}

After the client connects to the server, after obtaining the screenshot, set the size and clarity of the picture, and send the screenshots in a loop.

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
 
 
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(, );
            Graphics g= (bt);
            while(true)//Send screenshots in loop            {
 
            MemoryStream ms = new MemoryStream();
            (new Point(0, 0), new Point(0, 0), );//Get screenshot            Image mm = SaveJpg(bt,10);//Set picture clarity            mm = GetWebImage(mm,360,240);//Change the screenshot size            (ms, );
 
            byte[] buffer = new byte[1024];
             = 0;  
          
            int end = 1;
            while (end != 0)
            {
                end= (buffer, 0, 1024);// End is zero means reading is completed                (buffer, , 0);//1024 bytes are sent each time            }
            string sendStr = "over!";//End information            byte[] bs = (sendStr); 
            (bs, , 0);//Send test information            ();
            }
            ();  
            ();
        }
        public static Socket connect(int port, string host)
        {
            IPAddress ip = (host);
            IPEndPoint ipe = new IPEndPoint(ip, port);//Convert ip and port into IPEndPoint instance            Socket c = new Socket(, , );//Create a Socket            ("Conneting...");
            try
            {
               
                (ipe);//Connect to the server                return c;
            }
            catch
            {
                (1000);
                connect(port, host);
                return c;
            
            }
 
        }
        public static Image SaveJpg(Image image, long value)//Set image quality 1-100        {
            ImageCodecInfo icInfo = null;
            ImageCodecInfo[] infos = ();
            foreach (ImageCodecInfo info in infos)
            {
                if ( == "image/jpeg")
                {
                    icInfo = info;
                    break;
                }
            }
            EncoderParameters ep = new EncoderParameters(2);
            [0] = new EncoderParameter(, value);//Quality, define the clarity of the picture            [1] = new EncoderParameter(, (long));//Compression seems to have no effect            return image;
        }
        public static Image GetWebImage(Image img, int p_Width, int p_Height)//Change the image size        {
            Bitmap _Bitmap = new Bitmap(p_Width, p_Height);
            Graphics _Graphcis = (_Bitmap);
            _Graphcis.DrawImage(img, 0, 0, p_Width, p_Height);
            _Graphcis.Dispose();
            return _Bitmap;
        }
 
    }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.