基于C#编写一个远程桌面应用

 更新时间:2023年10月24日 08:27:28   作者:每日出拳老爷子  
封闭环境无法拷贝外来的远程桌面软件,所以这篇文章小编就来带大家用C#编写一个简单的远程桌面应用,感兴趣的小伙伴可以跟随小编一起学习一下

背景

封闭环境无法拷贝外来的远程桌面软件,所以就直接自己用C#写一个。

效果

说明

本篇会给出完整的编程步骤,照着写就能拥有你自己的远程桌面应用,直接可以运行在局域网。

如果不想自己敲代码,也可以选择直接下载项目资源,解包后直接用VS2019打开即可运行或自行打包成exe

设计

远程桌面需要一个服务端,一个客户端,各自是一个项目文件。
本项目中客户端分享画面(发送截屏数据流),服务端则是监听并接收画面,因此服务端需要两个Form(窗体)。

项目源码

客户端UI

只需要一个Form1,布局如下:

具体组件和属性设置如下:

  • Label1,text改为IP即可;
  • Label2,text改为Port即可;
  • textbox1,名称改为txtIP;
  • textbox2,名称改为txtPort,text改为80
  • button1,text改为Connect,名称改为btnConnect
  • button2,text改为ShareScreen,名称改为btnShare

客户端源码

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();
        }
    }
}

服务端UI Form1

  • textBox1,name设置为txtPort
  • button1,name设置为btnListen

Form1代码

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

源项目中追加一个窗体。

一个窗体里放一个imageBox,mode设置为zoom,dock设置为停靠。

Form2源码

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)
        {
        }
    }
}

到此这篇关于基于C#编写一个远程桌面应用的文章就介绍到这了,更多相关C#远程桌面内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C# 中的var关键字详细介绍

    C# 中的var关键字详细介绍

    这篇文章主要介绍了C# 中的var关键字详细介绍的相关资料,需要的朋友可以参考下
    2016-12-12
  • C#/VB.NET创建PDF文档的示例代码

    C#/VB.NET创建PDF文档的示例代码

    通过代码创建 PDF 文档有许多好处,所以本文将为大家详细介绍一下如何使用 Spire.PDF for .NET 在 C# 和 VB.NET 中从头开始创建 PDF 文档,需要的可以参考下
    2023-12-12
  • C#使用StopWatch获取程序毫秒级执行时间的方法

    C#使用StopWatch获取程序毫秒级执行时间的方法

    这篇文章主要介绍了C#使用StopWatch获取程序毫秒级执行时间的方法,涉及C#操作时间的相关技巧,需要的朋友可以参考下
    2015-04-04
  • C#实现将DataTable快速导出为Word表格

    C#实现将DataTable快速导出为Word表格

    在现代C#应用开发中,将程序数据以结构化的形式导出为用户友好的Word文档是一项常见的需求,下面我们来看看C#如何使用Spire.Doc for .NET库实现将DataTable中的数据导出到Word文档
    2026-01-01
  • Unity游戏脚本开发的生命周期函数详解(Update/FixedUpdate)

    Unity游戏脚本开发的生命周期函数详解(Update/FixedUpdate)

    本文介绍了Unity游戏开发中脚本的生命周期函数Update/FixedUpdate的核心概念、技术原理、应用场景、实践应用、常见问题与解决方案及最佳实践等内容,通过学习,可以掌握关键技术要点,提升游戏开发效率和项目质量
    2026-05-05
  • c#日期间隔计算示例

    c#日期间隔计算示例

    这篇文章主要介绍了c#日期间隔计算类,最后有使用方法,需要的朋友可以参考下
    2014-02-02
  • C#中自定义事件和委托实例

    C#中自定义事件和委托实例

    这篇文章主要介绍了C#中自定义事件和委托实例的,本文先是阐述了事件的原理,然后讲解了事件和委托的步骤,并给出了实例代码,需要的朋友可以参考下
    2015-01-01
  • WPF实现带筛选功能的DataGrid

    WPF实现带筛选功能的DataGrid

    在默认情况下,WPF提供的DataGrid仅拥有数据展示等简单功能,如果要实现像Excel一样复杂的筛选过滤功能,则相对比较麻烦。本文以一个简单的小例子,简述如何通过WPF实现DataGrid的筛选功能,仅供学习分享使用,如有不足之处,还请指正
    2023-03-03
  • C#实现Base64编码与解码及规则

    C#实现Base64编码与解码及规则

    这篇文章主要介绍了C#实现Base64编码与解码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-08-08
  • c# GridControl的模糊查询实现代码

    c# GridControl的模糊查询实现代码

    这篇文章主要介绍了c# GridControl的模糊查询实现代码,需要的朋友可以参考下
    2017-02-02

最新评论