Click here to Skip to main content
14,269,869 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello All,

I am using this particular function for compressing Jpeg images. But Instead of compressing those images, this function is increasing the size of byte array. Earlier this particular function was working fine , but now its not. Please help!!!
Thanks in advance for your help!!
This is the compress and Decompress code:

public static byte[] Compress(byte[] data)
            var compressedStream = new MemoryStream();
            var zipStream = new GZipStream(compressedStream, CompressionMode.Compress);
            zipStream.Write(data, 0, data.Length);
            return compressedStream.ToArray();

public static byte[] Decompress(byte[] data)
        const int BUFFER_SIZE = 256;
        byte[] tempArray = new byte[BUFFER_SIZE];
        List<byte[]> tempList = new List<byte[]>();
        int count = 0, length = 0;

        MemoryStream ms = new MemoryStream(data);
        DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);

        while ((count = ds.Read(tempArray, 0, BUFFER_SIZE)) > 0)
            if (count == BUFFER_SIZE)
                tempArray = new byte[BUFFER_SIZE];
                byte[] temp = new byte[count];
                Array.Copy(tempArray, 0, temp, 0, count);
            length += count;

        byte[] retVal = new byte[length];

        count = 0;
        foreach (byte[] temp in tempList)
            Array.Copy(temp, 0, retVal, count, temp.Length);
            count += temp.Length;
        return retVal;
Updated 29-Apr-10 2:49am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Create a bitmap. Save it as a BMP. That's how big an image is. Now save it as a jpg. What happens to the size ?

A JPEG is heavily compressed, in fact it's a lossy compression, it throws away data to help make the image even smaller. Compressing it again will add headers for the new compression routine but is very unlikely to make the core data any smaller than it is.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Please don't press 'answer' to ask more questions, edit your post. Yes, a jpg is probably as small as an image is going get. You could try a GIF if 256 color is fine for your images, but that's all I can think of that may be a bit smaller.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hello Christian,

Thanks for the reply. As a matter of fact , I am scanning the page as a bit map image and saving it as JPEG. After this I am trying to compress the JPEG image.

So per you, I should just scan the image in Bitmap and save it in JPEG. Is that it?
Also I would be transferring this image through a web service on server. That's the reason I would want to compress this image further and then transmit it over web. Since the huge size of the images is slowing down my application.

Could you also guide about the same.

Thanks for your help in advance.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100