几乎花了3个小时才找出问题所在。如果你看原始图像的大小和提取的图像,有一个微小的差异。
using (var memoryStream = new MemoryStream())
{
using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
for (var i = 0; i < images.Length; i++)
{
var img = images[i];
var file = zip.CreateEntry(i + ".bmp");
using (var stream = new MemoryStream())
{
img.Save(stream, ImageFormat.Bmp);
using (var entryStream = file.Open())
{// to keep it as image better to have it as bytes
var bytes = stream.ToArray();
entryStream.Write(bytes, 0, bytes.Length);
}
}
}
}
using (var fileStream = new FileStream(@"test.zip", FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
我试过了,效果很好!