我曾尝试使用 DotNetZip 将 .PNG 图像文件压缩为 zip 文件,但在我稍后将其提取并使用 Image.FromFile 方法后,它给了我一个 OutOfMemory 异常。我尝试了其他图像,但它给了我相同的结果。如果我用未压缩的文件覆盖图像,我不会收到该错误。
压缩代码
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = system.Abriviation + " Game|*" + system.Extention;
sfd.AddExtension = true;
sfd.ShowDialog();
if (sfd.FileName != "")
{
StreamWriter write = new StreamWriter(Application.StartupPath + "\\meta");
write.WriteLine(txtName.Text);
write.WriteLine(new FileInfo(txtGame.Text).Extension);
write.WriteLine(txtCompany.Text);
write.Close();
File.Copy(txtGame.Text, Application.StartupPath + "\\game" + new FileInfo(txtGame.Text).Extension);
File.Copy(txtGame.Text, Application.StartupPath + "\\icon.png");
ZipFile zip = new ZipFile();
zip.AddFile(Application.StartupPath + "\\meta", "");
zip.AddFile(Application.StartupPath + "\\game" + new FileInfo(txtGame.Text).Extension, "");
zip.AddFile(Application.StartupPath + "\\icon.png", "");
zip.Save(sfd.FileName);
zip.Dispose();
File.Delete(Application.StartupPath + "\\game" + new FileInfo(txtGame.Text).Extension);
File.Delete(Application.StartupPath + "\\icon.png");
File.Delete(Application.StartupPath + "\\meta");
Close();
}
提取码
ZipFile zip = ZipFile.Read(file);
bool foundMeta = false;
String id = "";
foreach(ZipEntry e in zip){
if (e.FileName == "meta")
{
e.Extract(Application.StartupPath);
StreamReader read = new StreamReader(Application.StartupPath + "\\meta");
id = read.ReadLine();
read.Close();
File.Delete(Application.StartupPath + "\\meta");
foundMeta = true;
break;
}
}
if (foundMeta)
{
if (!GameSystem.Exists(id))
{
zip.ExtractAll(Main.Library.Path + "\\" + id.Replace(" ", "_").ToLower());
Directory.CreateDirectory(Main.Library.Path + "\\" + id.Replace(" ", "_").ToLower() + "\\Games");
Directory.CreateDirectory(Main.Library.Path + "\\" + id.Replace(" ", "_").ToLower() + "\\Games\\metas");
}
else
{
MessageBox.Show("System " + id + " is already added.", "8 Bit", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Could not find the meta file.", "8 Bit", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
zip.Dispose();
return GameSystem.Load(Main.Library.Path + "\\" + id.Replace(" ", "_").ToLower());
图片加载代码
String name = "", company = "", ext = "";
FileInfo info = new FileInfo(file);
StreamReader read = new StreamReader(file);
name = read.ReadLine();
ext = read.ReadLine();
company = read.ReadLine();
read.Close();
Image icon = null;
if (File.Exists(system.GameMetaPath + "\\" + info.Name + ".png"))
{
icon = Image.FromFile(system.GameMetaPath + "\\" + info.Name + ".png");
//The error happens on the line above.
}
return new Game(system, name, company, system.GamePath + "\\" + info.Name + ext, file, icon);
有没有办法绕过或解决这个问题?