2

我正在尝试压缩位图图像,但输出流始终为空。

有谁知道我做错了什么?

提前致谢!

public static byte[] CompressBitmap (Bitmap img)
    {
        using (MemoryStream outputStream = new MemoryStream ())
        {
            // Compress image
            using (GZipStream zipStream = new GZipStream (outputStream, CompressionMode.Compress))
            {
                // Convert image into memory stream and compress
                using (MemoryStream imgStream = new MemoryStream ())
                {
                    img.Save (imgStream, System.Drawing.Imaging.ImageFormat.Bmp);                                                        
                    imgStream.CopyTo (zipStream);
                    return outputStream.ToArray ();
                }
            }
        }
    }

我改变了一点,似乎它的工作:

public static byte[] CompressBitmap (Bitmap img)
    {
        // Convertendo bitmap para memory stream
        using (MemoryStream inStream = new MemoryStream ())
        {
            // Para evitar memory leak
            using (img) 
            {
                img.Save(inStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }

            // Salva stream no array de byte
            byte[] buffer = new byte[inStream.Length];
            inStream.Read (buffer, 0, buffer.Length);

            // Comprime bitmap
            using (MemoryStream outStream = new MemoryStream())
            {
                using (GZipStream gzipStream = new GZipStream(outStream, CompressionMode.Compress))
                {
                    gzipStream.Write(buffer, 0, buffer.Length);
                    return outStream.ToArray();
                }
            }
        }            
    }

但是现在,解压错了:

public static Bitmap DecompressBitmap (byte[] compressed)
    {
        try
        {
            using (MemoryStream inputStream = new MemoryStream (compressed))
            {
                using (GZipStream zipStream = new GZipStream (inputStream, CompressionMode.Decompress))
                {
                    // Decompress image and convert to bitmap
                    using (MemoryStream outputStream = new MemoryStream ())
                    {
                        zipStream.CopyTo (outputStream);
                        return (Bitmap)Bitmap.FromStream (zipStream);
                    }
                }
            }
        }
        catch (Exception ex)
        {

        }

        return null;
    }
4

1 回答 1

2

我不知道您是否需要明确地将 BMP 转换为 JPEG。如果您只想压缩它、存储它并能够解压缩它,这将对您有所帮助。我在本地机器上用一个小屏幕截图进行了测试,它从 2.8MB 缩小到了 19KB,我可以使用这段代码并使用 7zip 打开它。

void Main()
{
    //set some test paths
    string originalfilepath = @"c:\temp\screenshot.bmp";
    string decompressedfilepath = @"c:\temp\screenshot.bmp.gz.bmp";
    string compressedfilepath = @"c:\temp\screenshot.bmp.gz";

    //read in the original file, this byte array could come from anywhere
    byte[] originalfilebytes = File.ReadAllBytes(originalfilepath);
    Console.WriteLine("original array size {0} bytes (as read from original file)",originalfilebytes.Length);

    //compress it
    byte[] compressedfilebytes = Compress(originalfilebytes);
    Console.WriteLine("compressed array size {0} bytes",compressedfilebytes.Length);

    //write it out so we can see it and test the compression with 7zip etc
    File.WriteAllBytes(compressedfilepath,compressedfilebytes);

    //write over our compressed bytes array by reading in the compressed file just to test
    compressedfilebytes = File.ReadAllBytes(compressedfilepath);
    Console.WriteLine("compressed array size {0} bytes (as read from compressed file)",compressedfilebytes.Length);

    //decompress the array
    byte[] decompressedfilebytes = Decompress(compressedfilebytes);
    Console.WriteLine("decompressed array size {0} bytes",decompressedfilebytes.Length);

    //write it to yet another file just to be sure decompress works
    File.WriteAllBytes(decompressedfilepath,decompressedfilebytes);
}
byte[] Compress(byte[] b)
{
using (MemoryStream ms = new MemoryStream())
{
    using (GZipStream z = new GZipStream(ms, CompressionMode.Compress, true))
        z.Write(b, 0, b.Length);
    return ms.ToArray();
}
}
byte[] Decompress(byte[] b)
{
    using (var ms = new MemoryStream())
    {
        using (var bs = new MemoryStream(b))
        using (var z = new GZipStream(bs, CompressionMode.Decompress))
            z.CopyTo(ms);
        return ms.ToArray();
    }
}
于 2013-03-27T15:56:13.543 回答