C# 用GZipStream压缩位图
我试图压缩位图图像,但输出流总是空的 有人知道我做错了什么吗 提前谢谢C# 用GZipStream压缩位图,c#,C#,我试图压缩位图图像,但输出流总是空的 有人知道我做错了什么吗 提前谢谢 public static byte[] CompressBitmap (Bitmap img) { using (MemoryStream outputStream = new MemoryStream ()) { // Compress image using (GZipStream zipStream = new GZipStrea
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();
}
}
为什么要压缩已经压缩的图像?因为我在Web服务上填写压缩图像参数。为了避免用位图填充,我意识到如果我传递一个字节数组(而不是一个太大的base64string)会更好,你解释的逻辑没有意义。实际上我在想:“哪个解决方案填充一个参数更好?抛出一个webservice?”所以我想把位图转换成base64string,但它太大了。然后,现在我尝试测试一个压缩位图,并将一个字节数组传递给WS。你们怎么想?不是个好主意?我想你误解了编码和压缩的区别。谢谢罗伊!我会尝试使用这种方法!我的荣幸。希望对你有用。如果对您有效,请将此标记为已回答,以便从未回答列表中筛选出来。=)