[C#][Source code] Directly available remote desktop application

【background】

A closed environment cannot copy foreign remote desktop software, so I just write one myself in C#.

【Effect】

Insert image description here

【illustrate】

This article will give complete programming steps. If you follow it, you can have your own remote desktop application, which can run directly on the LAN.
If you don’t want to type the code yourself, you can also choose to download the project resources directly. After unpacking, open it directly with VS2019 to run or package it into an exe yourself:
https://download.csdn.net/download/weixin_41697242/88350078

【design】

Remote desktop requires a server and a client, each of which is a project file.
In this project, the client shares the screen (sends a screenshot data stream), and the server monitors and receives the screen, so the server requires two Forms.

【Project source code】

Client UI

Only one Form1 is needed, and the layout is as follows:
Insert image description here
The specific components and attribute settings are as follows:
Label1, change the text to IP;
Label2, change the text to Port;
textbox1, change the name to txtIP;
textbox2, change the name to txtPort, and change the text to 80
button1, text changed to Connect, name changed to btnConnect
button2, text changed to ShareScreen, name changed to btnShare

Client source code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net.Sockets;
using System.Drawing.Imaging;
using System.Runtime.Serialization.Formatters.Binary;

namespace OriginalClient
{
    
    
    public partial class Form1 : Form
    {
    
    
        private readonly TcpClient client = new TcpClient();
        private NetworkStream mainStream;
        private int portNumber;

        private static Image GrabDesktop()
        {
    
    
            Rectangle bound = Screen.PrimaryScreen.Bounds;
            Bitmap screenshot = new Bitmap(bound.Width, bound.Height, PixelFormat.Format32bppArgb);
            Graphics graphics = Graphics.FromImage(screenshot);
            graphics.CopyFromScreen(bound.X, bound.Y, 0, 0, bound.Size, CopyPixelOperation.SourceCopy);

            return screenshot;
        }

        private void SendDesktopImage()
        {
    
    
            BinaryFormatter binFormatter = new BinaryFormatter();
            mainStream = client.GetStream();
            binFormatter.Serialize(mainStream, GrabDesktop());
        }

        public Form1()
        {
    
    
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
    
    
            btnShare.Enabled = false;

        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
    
    
            portNumber = int.Parse(txtPort.Text);
            try
            {
    
    
                client.Connect(txtIP.Text, portNumber);
                btnConnect.Text = "Connected";
                MessageBox.Show("Connected");
                btnConnect.Enabled = false;
                btnShare.Enabled = true;
            }
            catch (Exception)
            {
    
    
                MessageBox.Show("Failed to connect");
                btnConnect.Text = "Not Connected";
            }
        }

        private void btnShare_Click(object sender, EventArgs e)
        {
    
    
            if (btnShare.Text.StartsWith("Share"))
            {
    
    
                timer1.Start();
                btnShare.Text = "Stop Sharing";
            }
            else 
            {
    
    
                timer1.Stop();
                btnShare.Text = "Share My Screen";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
    
    
            SendDesktopImage();
        }
    }
}

Server UI

Form1

Insert image description here
textBox1, name is set to txtPort
button1, name is set to btnListen

Form1 code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OriginalServer
{
    
    
    public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
    
    

        }

        private void btnListen_Click(object sender, EventArgs e)
        {
    
    
            new Form2(int.Parse(txtPort.Text)).Show();
            btnListen.Enabled = false;
        }
    }
}
Form2

Append a form to the source project.
Place an imageBox in a form, set the mode to zoom, and set the dock to dock.

Form2 source code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;

namespace OriginalServer
{
    
    
    public partial class Form2 : Form
    {
    
    
        private readonly int port;
        private TcpClient client;
        private TcpListener server;
        private NetworkStream mainStream;

        private readonly Thread Listening;
        private readonly Thread GetImage;

        public Form2(int Port)
        {
    
    
            port = Port;
            client = new TcpClient();
            Listening = new Thread(StartListening);
            GetImage = new Thread(Receiveimage);


            InitializeComponent();
        }

        private void StartListening()
        {
    
    
            while (!client.Connected) 
            {
    
    
                server.Start();
                client = server.AcceptTcpClient();
            }
            GetImage.Start();
        }

        private void StopListening() 
        {
    
    
            server.Stop();
            client = null;
            if (Listening.IsAlive) Listening.Abort();
            if (GetImage.IsAlive) Listening.Abort();
        }

        private void Receiveimage()
        {
    
    
            BinaryFormatter binFormatter = new BinaryFormatter();
            while (client.Connected) 
            {
    
    
                mainStream = client.GetStream();
                pictureBox1.Image = (Image)binFormatter.Deserialize(mainStream);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
    
    
            base.OnLoad(e);
            server = new TcpListener(IPAddress.Any, port);
            Listening.Start();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
    
    
            base.OnFormClosing(e);
            StopListening();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
    
    

        }

        private void Form2_Load(object sender, EventArgs e)
        {
    
    

        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_41697242/article/details/132952125