gpt4 book ai didi

c# - 使用 GZipStream 压缩位图

转载 作者:太空宇宙 更新时间:2023-11-03 11:06:33 26 4
gpt4 key购买 nike

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

谁知道我做错了什么?

提前致谢!

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();
}
}

关于c# - 使用 GZipStream 压缩位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15661243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com