Bruno Klein
Bruno Klein

Reputation: 3367

Writing my own Screen Sharing Server and Protocol

I'm building a client/server solution which needs to have screen sharing functionality. I have something already "working" but the problem is that it only works over internal network, because my methodology is not fast enough.

What I am basically doing is that the client makes a request for the server asking for a screen image each 5 seconds (for example). And this is the code which is processed once this requests are received:

private void GetImage(object networkstream)
{
    NetworkStream network = (NetworkStream)networkstream;

    Bitmap bitmap = new Bitmap(
        SystemInformation.PrimaryMonitorSize.Width,
        SystemInformation.PrimaryMonitorSize.Height);
    Graphics g = Graphics.FromImage(bitmap);
    g.CopyFromScreen(new Point(0, 0), new Point(0, 0), bitmap.Size);
    g.Flush();
    g.Dispose();

    MemoryStream ms = new MemoryStream();
    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    bitmap.Dispose();

    byte[] array = ms.ToArray();

    network.Write(array, 0, array.Length);
    network.Flush();

    ms.Dispose();
}
  1. What are best methods to do what I'm trying to? I need to get at least 0.2 FPS (refresh each 5 seconds) Obs.: I'm using Windows Forms and it is being done over sockets.

  2. How do TeamViwer and .rdp files work?

Upvotes: 1

Views: 3208

Answers (3)

delixfe
delixfe

Reputation: 2491

What about using an existing implementation? Or learning from it? http://cdot.senecac.on.ca/projects/vncsharp/

Upvotes: 2

Sten Petrov
Sten Petrov

Reputation: 11040

You need to optimize your protocol, here are some suggestions:

  • break the input image in segments, send segments instead of full screen
  • only send a segment if it's different from the previously sent version
  • use http notification type of communication where your viewer sends a request but only gets a response if the server received new segments from the presenter, possibly several grouped together.
  • compress the image data, don't transmit raw
  • give users the option to choose the level of compression to speed things up or to get a better image
  • I doubt this would be in your budget but you can also encode the stream as streaming video

Upvotes: 2

Mateusz Krzaczek
Mateusz Krzaczek

Reputation: 614

You can send only difference betwen present and last image. Look here: Calculate image differences in C#

If it wont be fast enough, you can divide your screen into smallers, like 100x100 or 50x50 bitmaps, check if this area had changed and if yes just send it to client.

Upvotes: 2

Related Questions