压缩位图与GZipStream
本文关键字:GZipStream 位图 压缩 | 更新日期: 2023-09-27 18:06:01
我正在尝试压缩位图图像,但输出流始终为空。
有人知道我做错了什么吗?
提前感谢!
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;
}
我不知道你是否需要将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();
}
}