how to compress image size to byte array without a

2019-08-11 05:46发布

The problem is I want to send a desktop screenshot(which is Bitmap type) via UDP. As you know UDP has limited capacity to 65 000 bytes, so I have to "compress" image size to fit this capacity. I tried this:

MemoryStream memoryStream = new MemoryStream();
Bitmap bmp = CaptureDesktopWithCursor2();
        ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
        System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
        EncoderParameters myEncoderParameters = new EncoderParameters(1);
        EncoderParameter myEncoderParameter = new      EncoderParameter(myEncoder, 15L);
        myEncoderParameters.Param[0] = myEncoderParameter;
        bmp.Save(memoryStream, jpgEncoder, myEncoderParameters);
        Bitmap Bit;
                using (memoryStream)
        {

           Bit = new Bitmap(memoryStream);

            byte[] lol = imageToByteArray(Bit);
        }

It does change size of image, but only when you save it as .jpg file for example.

Is there anything else what I can do to fit UDP capacity(65 000)? Of course I tried resizing image(it fits cap well then) but it affects quality of this image much.

   public byte[] imageToByteArray(Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        return ms.ToArray();
    }

0条回答
登录 后发表回答