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 に答える