开发者

Simple C# Screen sharing application

开发者 https://www.devze.com 2023-01-08 08:08 出处:网络
I am looking to create a very basic screen sharing application in C#. No remote control necessary. I just want a user to be able to broadcast their screen to a webserver.

I am looking to create a very basic screen sharing application in C#. No remote control necessary. I just want a user to be able to broadcast their screen to a webserver.

How should I implement this? (Any pointer in the right direction 开发者_高级运维will be greatly appreciated).

It does NOT need to be high FPS. Would be sufficient to even update ever 5s or so. Do you think it would be sufficient to just upload a screenshot ever 5 seconds to my web server?


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

Simple C# Screen sharing application

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:

Simple C# Screen sharing application

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

精彩评论

暂无评论...
验证码 换一张
取 消