Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple C# Screen sharing application

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?

like image 226
Andy Hin Avatar asked Jul 20 '10 23:07

Andy Hin


2 Answers

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.

like image 44
Gabriel Magana Avatar answered Oct 12 '22 07:10

Gabriel Magana


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.

like image 157
Brian R. Bondy Avatar answered Oct 12 '22 09:10

Brian R. Bondy