Simple C# Screen sharing application

好久不见. 提交于 2019-12-02 18:47:39

I previously blogged about how remote screen sharing software works here, it is not specific to C# but it gives a good fundamental understanding on the topic. Also linked in that article is the remote frame buffer spec which you'll also probably want to read up on.

Basically you will want to take screenshots and you can transmit those screenshots and display them on the other side. You can keep the last screenshot and compare the screenshot in blocks to see which blocks of the screenshot you need to send. You would typically do some sort of compression before sending the data.

To have remote control you can track mouse movement and transmit it and set the pointer position on the other end. Also ditto about keystrokes.

As far as compression goes in C#, you can simply use JpegBitmapEncoder to create your screenshots with Jpeg compression with the quality that you want.

JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 40; 

To compare file blocks you are probably best to create a hash on the old block and the new one, and then check to see if they are the same. You can use any hashing algorithm you want for this.

Here's code to take a screenshot, uncompressed as a bitmap:

    public static Bitmap TakeScreenshot() {
        Rectangle totalSize = Rectangle.Empty;

        foreach (Screen s in Screen.AllScreens)
            totalSize = Rectangle.Union(totalSize, s.Bounds);

        Bitmap screenShotBMP = new Bitmap(totalSize.Width, totalSize.Height, PixelFormat.
            Format32bppArgb);

        Graphics screenShotGraphics = Graphics.FromImage(screenShotBMP);

        screenShotGraphics.CopyFromScreen(totalSize.X, totalSize.Y, 0, 0, totalSize.Size,
            CopyPixelOperation.SourceCopy);

        screenShotGraphics.Dispose();

        return screenShotBMP;
    }

Now just compress it and send it over the wire, and you're done.

This code combines all screens in a multiscreen setup into one image. Tweak as needed.

Well, it can be as simple as taking screenshots, compressing them, and then sending them over the wire. However, there is existing software that already does this. Is this for practice?

I'm looking to do something similar, and I just found this up on CodeProject. I think this will help you.

http://www.codeproject.com/Articles/371955/Motion-JPEG-Streaming-Server

The key player on sharing/replicating a screen is a COM Component called: RPDViewer

Add that com component to your window form and in References as well.. and thin add this code to your form load and you will get the screen replicated in your form:

using RDPCOMAPILib;
using System;
using System.Windows.Forms;

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

        RDPSession x = new RDPSession(); 
        private void Incoming(object Guest)
        {
            IRDPSRAPIAttendee MyGuest = (IRDPSRAPIAttendee)Guest; 
            MyGuest.ControlLevel = CTRL_LEVEL.CTRL_LEVEL_INTERACTIVE;
        }


        //access to COM/firewall will prompt 
        private void button1_Click(object sender, EventArgs e)
        {
            x.OnAttendeeConnected += Incoming;
            x.Open();
        }

        //connect
        private void button2_Click(object sender, EventArgs e)
        {
            IRDPSRAPIInvitation Invitation = x.Invitations.CreateInvitation("Trial", "MyGroup", "", 10);
            textBox1.Text = Invitation.ConnectionString;
        }

        //Share screen

        private void button4_Click(object sender, EventArgs e)
        {
            string Invitation = textBox1.Text;// "";// Interaction.InputBox("Insert Invitation ConnectionString", "Attention");
            axRDPViewer1.Connect(Invitation, "User1", "");
        }


        //stop sharing
        private void button5_Click(object sender, EventArgs e)
        {
            axRDPViewer1.Disconnect();
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!