Compress bitmap before sending over network

女生的网名这么多〃 提交于 2019-12-24 03:59:14

问题


I'm trying to send a bitmap screenshot over network, so I need to compress it before sending it. Is there a library or method for doing this?


回答1:


When you save an Image to a stream, you have to select a format. Almost all bitmap formats (bmp, gif, jpg, png) use 1 or more forms of compression. So just select an appropriate format, and make make sure that sender and receiver agree on it.




回答2:


Try the System.IO.DeflateStream class.




回答3:


If you are looking for something to compress the image in quality, here it is-

    private Image GetCompressedBitmap(Bitmap bmp, long quality)
    {
        using (var mss = new MemoryStream())
        {
            EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            ImageCodecInfo imageCodec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(o => o.FormatID == ImageFormat.Jpeg.Guid);
            EncoderParameters parameters = new EncoderParameters(1);
            parameters.Param[0] = qualityParam;
            bmp.Save(mss, imageCodec, parameters);
            return Image.FromStream(mss);
        }
    }

Use it -

var compressedBmp = GetCompressedBitmap(myBmp, 60L);



回答4:


May be you can use:

private Bitmap compressImage(Bitmap image) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 
        int options = 100;
        while ( baos.toByteArray().length / 1024>100) { // 
            baos.reset();
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 
            options -= 10;// 10
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 
        return bitmap;
    }


来源:https://stackoverflow.com/questions/3034167/compress-bitmap-before-sending-over-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!