Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I've been trying to develop Remote Desktop Program in C# and I want to make the BYTE size of my Image smaller. Here is my attempt below so far.

C#
//
                int w = Screen.PrimaryScreen.Bounds.Width;
                int h = Screen.PrimaryScreen.Bounds.Height;

                //
                Size s = new Size(w, h);

                Bitmap ScreenImage = new Bitmap(w, h);

                Graphics graphics = Graphics.FromImage(ScreenImage);
                graphics.CopyFromScreen(0, 0, 0, 0, s);

                int width = ScreenImage.Width / 2;
                int height = ScreenImage.Height / 2;
                Size resize = new Size(width, height);
                Bitmap resizeImage = new Bitmap(ScreenImage, resize);

                // Bitmap To byte[]
                MemoryStream ms = new MemoryStream();
                resizeImage.Save(ms, ImageFormat.Jpeg);
                ms.Position = 0;
                byte[] data = ms.ToArray();

                byte[] compressedByte = Compress(data);



    public static Byte[] Compress(Byte[] buffer)
    {
        Byte[] compressedByte;
        using (MemoryStream ms = new MemoryStream())
        {
            using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress))
            {
                ds.Write(buffer, 0, buffer.Length);
            }

            compressedByte = ms.ToArray();
        }

        return compressedByte;
    }


I tried to make the Bitmap Image resized and compressed to make its BYTE size smaller but I still need to make it smaller. Could anyone give me any other ways? Thank you in advance!

What I have tried:

Bitmap resizeImage = new Bitmap(ScreenImage, resize);
byte[] compressedByte = Compress(data);
Posted
Updated 28-May-19 9:32am
v2

Not really: JPEG files are already compressed, and further compression often makes them larger, not smaller. You're already using ZIP compression, but it's unlikely to produce significant gains on a JPEG file anyway. If you get an extra 10% then you were doing well.

But...you do realise that JPEG is a "lossy compression" format? Every time you save a file as JPEG you lose information (and generally get a smaller file) and a more "fuzzy" picture?
I'd suggest that you same it as a loss-less format (BMP (uncompressed, but ZIPs well) or PNG (compressed) for example) and compress that instead.
 
Share this answer
 
Comments
MinYoung Lee 30-May-17 5:45am    
Thank you, Griff. As you said about "lossy compression, fuzzy picture", My program suddenly stops sending Bitmap Images to Server in a few minutes since started. I think the reason for this is what you said exactly. I will use BMP or PNG instead.
[no name] 28-May-19 16:14pm    
.
As already noted you usually can't gain much trying to compress a JPEG image, which is alreay compressed. In your code you changed the image size, making it smaller. If it is acceptable to make it even more smaller, then you may save some memory.
 
Share this answer
 
Comments
MinYoung Lee 30-May-17 5:46am    
Thank you for some help, CPallini!
CPallini 30-May-17 5:49am    
You are welcome.
The imagem compression follow another objects and features that different of trivial files; image compreession has properties to equalize the compression. Like code bellow, the compression act on Quality property:


C#
static byte[] CompressByImageAlg(int jpegQuality, byte[] data)
        {
            using (MemoryStream inputStream = new MemoryStream(data))
            {
                using (Image image = Image.FromStream(inputStream))
                {
                    ImageCodecInfo jpegEncoder = ImageCodecInfo.GetImageDecoders()
                        .First(c => c.FormatID == ImageFormat.Jpeg.Guid);
                    var encoderParameters = new EncoderParameters(1);
                    encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, jpegQuality);
                    byte[] outputBytes = null;
                    using (MemoryStream outputStream = new MemoryStream())
                    {
                        image.Save(outputStream, jpegEncoder, encoderParameters);
                        return outputStream.ToArray();
                    }
                }
            }
        }
 
Share this answer
 
Comments
OriginalGriff 29-May-19 4:53am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 2 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.
Member 1408653 1-Feb-22 10:53am    
A bit of a grumpy jobsworth. It helped me solve my problem regardless of a 2 year gap. Isn't that what this site should be doing 🤔 Thank you AntonioLeonardo
Six Hat Solutions 17-Jan-22 10:26am    
@OriginalGriff What is this Stack Overflow? I can't believe you are getting on him about posting a legitimate answer on an old post! This type of response I would have expected from Stack Overflow but not here. This is exactly why I even limit trying to help on forums as you have to go through several rounds of "fault finding".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900