简体   繁体   English

C#和EmguCV:内存中的ZIP图像

[英]C# and EmguCV: ZIP image from memory

I want to add a set of images to a ZIP file in C#. 我想将一组图像添加到C#中的ZIP文件中。 I use EmguCV to save the images. 我使用EmguCV保存图像。 Currently I save the image locally first and then add to ZIP as follows. 目前,我先将图像保存在本地,然后按如下所示添加到ZIP中。

Is there anyway to add the images to ZIP file directly on the fly without saving locally? 无论如何,有没有在本地保存直接将图像直接添加到ZIP文件的方法?

Sorry if this is a duplicate, I could not locate a clear answer for days. 抱歉,如果重复的话,我几天都找不到明确的答案。

using (FileStream zipToOpen = new FileStream(this.path, FileMode.Create))
{
  using (ZipArchive zipArchive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
  {
    for (int i = 0; i < clouds.Count; i++)
    {
      string imageCname = kinectId + "_" + dateTimes[i] + ".jpg";
      imageCt = imageCs[i];
      imageCt.Save("imageCt.jpg");
      zipArchive.CreateEntryFromFile(@"imageCt.jpg", imageCname);
      Console.WriteLine("Flushing rgb " + i + " of " + clouds.Count);
    }
  }
}

Update 1: 更新1:

As suggested by #CarbineCoder I edited the code as follows. 如#CarbineCoder所建议,我按如下方式编辑了代码。 It saves a file. 保存文件。 But cannot open as an image. 但是不能作为图像打开。 I guess, it just save only the Byte steam. 我猜想,它只保存了Byte流。 What else may I do to save it as a jpeg image. 我还可以做什么将其另存为jpeg图像。

string imageCname = kinectId + "_" + dateTimes[i] + ".jpg";
imageCt = imageCs[i];
ZipArchiveEntry zipEntryC = zipArchive.CreateEntry(imageCname);

using (var originalFileStream = new MemoryStream(imageCt.Bytes))
{
  using (var zipEntryStream = zipEntryC.Open())
  {
    originalFileStream.CopyTo(zipEntryStream);
  }
}

Can you post the code you changed? 您可以发布更改的代码吗?

use zipArchive.CreateEntry() solutiuon specified in- 使用zipArchive.CreateEntry()解决方案-

Create zip file from byte[] 从字节创建zip文件[]

Creating a ZIP Archive in Memory Using System.IO.Compression 使用System.IO.Compression在内存中创建ZIP存档

Yes you might need to specify the extension of the file like zipArchive.CreateEntry("FileName.jpg") . 是的,您可能需要指定文件的扩展名,例如zipArchive.CreateEntry("FileName.jpg") This might solve your problem 这可能会解决您的问题

Finally resolved the issue with the CarbineCoder suggestion. 最终通过CarbineCoder建议解决了该问题。 Posted below is the working code. 以下是工作代码。

string imageCname = kinectId + "_" + dateTimes[i] + ".jpg";
imageCt = imageCs[i];

ZipArchiveEntry zipEntryC = zipArchive.CreateEntry(imageCname);

using (var stream = new MemoryStream())
{
  using (var zipEntryStream = zipEntryC.Open())
  {
    System.Drawing.Image userImage = imageCt.ToBitmap();
    userImage.Save(stream, ImageFormat.Jpeg);
    stream.Seek(0, SeekOrigin.Begin);
    stream.CopyTo(zipEntryStream);
  }
}
Console.WriteLine("Flushing rgb " + i + " of " + clouds.Count);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM