Search code examples
c#rdpscreensharing

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?


Solution

  • 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.