如何压缩和解压缩JPEG图像? [英] How do I compress and Decompress JPEG images?

查看:107
本文介绍了如何压缩和解压缩JPEG图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用此特定功能来压缩Jpeg图像.但是此功能不是压缩那些图像,而是增加字节数组的大小.较早之前,此特定功能运行良好,但现在却无法正常运行.请帮忙!!!
在此先感谢您的帮助!
这是压缩和解压缩代码:

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)
            {
                tempList.Add(tempArray);
                tempArray = new byte[BUFFER_SIZE];
            }
            else
            {
                byte[] temp = new byte[count];
                Array.Copy(tempArray, 0, temp, 0, count);
                tempList.Add(temp);
            }
            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;
        }
        ds.Close();
        return retVal;
    }

推荐答案

创建位图.将其另存为BMP.那就是一张图像有多大.现在将其另存为jpg.大小会怎样?

JPEG是经过高度压缩的,实际上是有损压缩,它会丢弃数据以帮助缩小图像.再次压缩将为新的压缩例程添加头文件,但不太可能使核心数据变得更小.
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.


请不要按"answer"询问还有更多问题,请修改您的信息.是的,jpg可能和图像一样小.如果您的图片适合256色,则可以尝试GIF,但是我能想到的可能会更小.
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.


Hello Christian,

谢谢回复.实际上,我将页面扫描为位图图像并将其另存为JPEG.之后,我尝试压缩JPEG图像.

因此,根据您的情况,我应该只在位图中扫描图像并将其保存为JPEG.是吗?
另外,我将通过服务器上的Web服务传输此图像.这就是我想要进一步压缩该图像然后通过Web传输它的原因.由于巨大的图像会减慢我的应用程序的速度.

您还能指导一下吗?

感谢您的提前帮助.
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.


这篇关于如何压缩和解压缩JPEG图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆