如何创建基本包来存储图像? [英] How to create a basic package to store images ?
问题描述
如何创建基本包来存储图像?像一个zip文件。我想要的只是在一个包中存储20000个图像。我的硬盘更容易。此外,我需要能够使用c#代码进入和退出该包,读/写,添加或删除文件。另一种文件格式是.iso,它接近我想要的,但操作起来很复杂。我想要一些非常基本但不复杂的东西。如果可能,基本作为库。有这样的事吗?谢谢!
我的尝试:
与这种情况。
How to create a basic package to store images ? Like a zip file. All i want is to store 20000 images inside one package. Is easier for my hard disk. Also, I need to be able to enter and get out from that package, to read/write , add or remove files, using c# code. Another file format is .iso that is close to what I want, but is complicated to operate with it. I want something very basic, not complicated. Basic as a library if possible. Is there such a thing? Thank you!
What I have tried:
not relevant in this situation.
推荐答案
您可以在.NET中创建和管理zip文件,参见ZipFile Class(System.IO.Compression) [ ^ ]。
You can create and manage zip files in .NET, see ZipFile Class (System.IO.Compression)[^].
使用.NET 4.5提供的System.IO.Compression中的ZipArchive类:[ ^ ]。
Use the ZipArchive Class in System.IO.Compression available with .NET 4.5: [^].
Create a zip archive from a directory
ZipFile.CreateFromDirectory
Extract the contents of a zip archive to a directory
ZipFile.ExtractToDirectory
Add new files to an existing zip archive
ZipArchive.CreateEntry
Retrieve a file from a zip archive
ZipArchive.GetEntry
ZipArchive.Entries
Retrieve all the files from a zip archive
ZipArchiveEntry.Open
Open a stream to a single file contained in a zip archive
ZipArchiveEntry.Delete
Delete a file from a zip archive
谢谢大家的支持输入,它帮助我决定并指导我在许多网络搜索战斗后找到这个答案。
我找到了一个实用的解决方案,但不是那个我希望它有效率。
从zip文件中循环播放图像时移动速度慢,因为它正在解压缩每个文件。我必须重新考虑代码并将其全部解压缩到流或某些列表中。我再看看吧。现在,正在工作,我很高兴:)
这是我想出的结果:
Thank you all for your input, it helped me decide and it direction me to this answer I find after many web search battles.
I find a practical solution, but not that efficient as I want it.
Is moving slow-ish when cycling the images from inside a zip file, because it is unpacking each of them. I must re-think the code and unzip all into a stream or some lists. I will see. For now, is working and I am very happy :)
Here is the result I came up with:
//My code so far - not very efficient but is working.
using Ionic.Zip;
using Ionic.Zlib;
string zipPath = "0Images.zip";
void CountZipFiles()
{
using (ZipFile zip = new ZipFile(zipPath))
{
totalzipFiles = zip.Count-1;
}
}
Image emptyImage = Image.FromFile("emptyFemale.jpg");
void ReadZipImage()
{
using (ZipFile zip = new ZipFile(zipPath))
{
MemoryStream tempS = new MemoryStream();
for (int i = 0; i < zip.Count; i++)
{
if (i == countMyZipImages)
{
label1.Text = zip[i].FileName;
if (zip[i].FileName.Contains(".niet"))
{
pictureBox1.Image = emptyImage;
}
else
{
zip[i].Extract(tempS);
pictureBox1.Image = Image.FromStream(tempS);
}
}
}
}
}
int totalzipFiles = 0, countMyZipImages = 0;
private void button2_Click(object sender, EventArgs e)
{
countMyZipImages--;
if (countMyZipImages < 0) countMyZipImages = totalzipFiles;
textBox1.Text = countMyZipImages.ToString();
ReadZipImage();
}
private void button3_Click(object sender, EventArgs e)
{
countMyZipImages++;
if (countMyZipImages > totalzipFiles) countMyZipImages = 0;
textBox1.Text = countMyZipImages.ToString();
ReadZipImage();
}
// and this is a HELP file for later use - hopefully will help others too. ;)
How to add Ionic.Zip.dll in c#.net project and use it:
To add a reference, right click (in Solution Explorer on your project) Reference folder and select Add Reference.
Then browse and add the file Ionic.Zip.dll
//Important to add this using's too after referencing.
using Ionic.Zip;
using Ionic.Zlib;
private void CreateZIP_Click(object sender, EventArgs e)
{
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
// add the report into a different directory named "files" in the archive
zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
zip.AddFile("ReadMe.txt");
zip.Save("MyZipFile.zip");
Exception ex = new Exception();
label1.Text = ex.Message;
}
}
//You can extract to a stream, or a fizical file !
private void button5_Click(object sender, EventArgs e)
{
using (ZipFile zip = new ZipFile("0Images.zip"))
{
MemoryStream tempS = new MemoryStream(); //stream
//{
foreach (ZipEntry ze in zip) //foreach
{
// check if you want to extract the image.name
if (ze.FileName == "00002 Riley Reid.jpg")
{
ze.Extract(tempS);
pictureBox1.Image = Image.FromStream(tempS);
}
}
//OR
for (int i = 0; i < zip.Count; i++) //for
{
if (i == countMyZipImages)
{
zip[i].Extract(tempS);
pictureBox1.Image = Image.FromStream(tempS);
}
}
//}
}
}
这是我在互联网上找到的免费图书馆!我喜欢它,因为它很少 - 435kb。如果他们想要使用它,我会找到其他链接。 Dropbox - Ionic.Zip.dll [ ^ ]
这篇关于如何创建基本包来存储图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!