3

I am working on screen sharing project. I am sending only screen differences over socket comparing previous and actual buffer. It working

I am sending 8 to 9 FPS to client using Format16bppRgb555 to reduce overall bytes size of Bitmap

byte[] wholescreensize= new byte[1360 * 768 * 2];// Its around 2 Mb

My problem Is when full screen is changed.

I am getting about 45-60 kb of PNG image using below function

45kb * 10 (FPS) = 450 kb

It is possible to reduce beyond 45 kb.

I am not interested to reduce FPS as it live screen sharing app.

JPEG Compression or LZ4/GZIP also not making much difference as PNG image already compressed

private void SendImgDiffToClient(byte[] contents,Rectangle rectangle)
{   

    //Converting Small Portion to Bitmap.Bcoz Image.FromStrem not working here error Parameter is not Valid
    byte[] byteArrayout = new byte[contents.Length];

    var bitmap = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format16bppRgb555);
    var bitmap_data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format16bppRgb555);
    Marshal.Copy(contents, 0, bitmap_data.Scan0, byteArrayout.Length);
    bitmap.UnlockBits(bitmap_data);

    //Converting Small Bitmap to Png Byte Array and Sending to Client
    using (MemoryStream ms = new MemoryStream())
    { 
        Image msImage = (Image)bitmap;
        msImage.Save(ms, ImageFormat.Png);

        msImage.Dispose();
        byteArrayout = ms.ToArray();
    }  

    SendtoClient(byteArrayout);
}

My Questing is what is a best approach to reduce bytes in such scenario.

20
  • PNG is lossless compression. If you want to reduce it even further, use lossy (JPG) compression, inter-frame compression (mpg) and/or lower the framerate. Commented Mar 5, 2018 at 10:25
  • 1
    There is no good way to do this, always too slow for modern monitor sizes. Use the built-in OS support for this, google "c# remote desktop services" to find hits. It gets its speed by encoding video driver commands instead of pixels. Commented Mar 5, 2018 at 10:31
  • @HansPassant i am already researching for this. i am not getting any appropriate solution for that. tried this but no luck stackoverflow.com/questions/31543940/… Commented Mar 5, 2018 at 10:34
  • 2
    Well, I told you so. Use RDP. Commented Mar 5, 2018 at 10:36
  • 3
    Then, github.com/T1T4N/NVNC could inspire you :) Commented Mar 7, 2018 at 13:36

1 Answer 1

2
+25

Video streaming is essentially what you're doing; and modern video compression algorithms have lots of enhancements. Perhaps they can track or move an artifact, or otherwise distort said artifact as part of their functionality. Perhaps they can stream the data in a progressively building manner, so that static items eventually acquire more detail (similar to progressive jpeg images.) They do lots of things all at the same time. You can try to research them further, and take inspiration from them, or you could pick and use one.

This is to say that many people here seem to prefer the solution of using a readily available video compression library. Especially if you are worried about streaming bandwidth.

If you don't want to use an existing video library, then you have to decide how much effort you want to put in, versus how sloppy you want to be with consuming more bandwidth than otherwise necessary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.