为什么DotNetZip会导致图像中的OutOfMemory异常?

时间:2013-08-08 01:38:40

标签: c# image dotnetzip

我已尝试使用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);

有没有办法解决或解决这个问题?

0 个答案:

没有答案