C#语言和Windows Forms应用程序写一个远程桌面工具

2023年2月23日15:26:20 评论 588
首月免费体验

要实现一个远程桌面工具,可以使用C#语言和Windows Forms应用程序。以下是一个简单的示例代码,可以使用TCP协议在两个计算机之间建立连接,并在客户端计算机上显示远程计算机的屏幕:

Server端代码:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace RemoteDesktopServer
{
    public partial class ServerForm : Form
    {
        private TcpListener tcpListener;
        private TcpClient tcpClient;
        private Thread listenThread;
        private Thread sendThread;
        private bool isRunning = false;

        public ServerForm()
        {
            InitializeComponent();
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            isRunning = true;
            listenThread = new Thread(new ThreadStart(ListenForClients));
            listenThread.Start();
        }

        private void ListenForClients()
        {
            try
            {
                tcpListener = new TcpListener(IPAddress.Any, 8888);
                tcpListener.Start();
                Invoke(new Action(() =>
                {
                    StatusLabel.Text = "Waiting for client...";
                }));

                tcpClient = tcpListener.AcceptTcpClient();
                Invoke(new Action(() =>
                {
                    StatusLabel.Text = "Client connected!";
                }));

                sendThread = new Thread(new ThreadStart(SendScreen));
                sendThread.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void SendScreen()
        {
            try
            {
                while (isRunning)
                {
                    Rectangle bounds = Screen.GetBounds(Point.Empty);
                    Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
                    Graphics graphics = Graphics.FromImage(bitmap);
                    graphics.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size, CopyPixelOperation.SourceCopy);

                    NetworkStream stream = tcpClient.GetStream();
                    byte[] imageData = ImageToByteArray(bitmap);
                    stream.Write(imageData, 0, imageData.Length);
                    Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private byte[] ImageToByteArray(Image image)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                image.Save(stream, ImageFormat.Jpeg);
                return stream.ToArray();
            }
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            isRunning = false;
            if (tcpClient != null)
            {
                tcpClient.Close();
            }
            if (tcpListener != null)
            {
                tcpListener.Stop();
            }
            if (listenThread != null)
            {
                listenThread.Join();
            }
            if (sendThread != null)
            {
                sendThread.Join();
            }
            StatusLabel.Text = "Server stopped.";
        }
    }
}

 

Client端代码:

using System;
using System.Drawing;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace RemoteDesktopClient
{
    public partial class ClientForm : Form
    {
        private TcpClient tcpClient;
        private Thread receiveThread;
        private bool isRunning = false;

        public ClientForm()
        {
            InitializeComponent();
        }

        private void ConnectButton_Click(object sender, EventArgs e)
        {
            try
            {
                tcpClient = new TcpClient();
                tcpClient.Connect(IPAddress.Parse(IpAddressTextBox.Text), 888

 

 

 

 

 

 

 

 

 

 

 

 

weinxin
博主微信
这是我的微信扫一扫

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: