gpt4 book ai didi

C# 将图像写入 ziarchive

转载 作者:行者123 更新时间:2023-11-30 23:11:54 29 4
gpt4 key购买 nike

我正在编写一个 C# .NET(v4.6.1) WinForms 应用程序,它将文件保存为 System.IO.Compression.ZipArchive。首先,我使用 SaveFileDialog() 方法打开图像,然后将其分配给 System.Drawing.Image 类型的变量。然后我保存文件,从字符串、整数和 bool 值开始进入一个文本文件。接下来,我尝试从变量而不是从硬盘上的原始文件路径保存图像。这是保存文件的代码,文本文件后跟图像:

     public void SaveStd2Zip(string strfilepath, List<clsStandardQuestion> lstQuestions,
clsProjectInfo GameInfo, List<clsMedia> lstProjMed)
{
using (var ms = new MemoryStream())
{
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
var projectInfo = archive.CreateEntry("ProjectInfo.txt");

using (var entryStream = projectInfo.Open())
using (var sw = new StreamWriter(entryStream))
{
sw.WriteLine(GameInfo.strGameVersion);
sw.WriteLine(GameInfo.strProjectType);
sw.WriteLine(GameInfo.strGameTitle);
sw.WriteLine(GameInfo.strAuthor);
sw.WriteLine(GameInfo.strCreationDate);
sw.WriteLine(GameInfo.blTSImagePresent);
sw.WriteLine(GameInfo.blTSAudioPresent);
sw.WriteLine(GameInfo.blTSVideoPresent);
sw.WriteLine(GameInfo.blFSSImagePresent);
sw.WriteLine(GameInfo.blFSSAudioPresent);
sw.WriteLine(GameInfo.blFSSVideoPresent);
sw.WriteLine(GameInfo.intTotalQuestions);
sw.WriteLine(GameInfo.intTotalMediaItems);
sw.WriteLine(GameInfo.intTotalCategories);
sw.WriteLine(GameInfo.blTiebreakerPresent);

if (GameInfo.intTotalQuestions > 0)
{
for (int i = 0; i > lstQuestions.Count; i++)
{
sw.WriteLine(lstQuestions[i].blIsTiebreaker);
sw.WriteLine(lstQuestions[i].strQuestion);
sw.WriteLine(lstQuestions[i].intPoints);
sw.WriteLine(lstQuestions[i].intQuestionType);
sw.WriteLine(lstQuestions[i].blQuestionImagePresent);
sw.WriteLine(lstQuestions[i].blQuestionAudioPresent);
sw.WriteLine(lstQuestions[i].blQuestionVideoPresent);
sw.WriteLine(lstQuestions[i].blIncludeTimer);
sw.WriteLine(lstQuestions[i].intTimerTime);

// Multiple Choice variables...
sw.WriteLine(lstQuestions[i].blAIsChecked);
sw.WriteLine(lstQuestions[i].strAnswerA);
sw.WriteLine(lstQuestions[i].blAIsCorrect);
sw.WriteLine(lstQuestions[i].blBIsChecked);
sw.WriteLine(lstQuestions[i].strAnswerB);
sw.WriteLine(lstQuestions[i].blBIsCorrect);
sw.WriteLine(lstQuestions[i].blCIsChecked);
sw.WriteLine(lstQuestions[i].strAnswerC);
sw.WriteLine(lstQuestions[i].blCIsCorrect);
sw.WriteLine(lstQuestions[i].blDIsChecked);
sw.WriteLine(lstQuestions[i].strAnswerD);
sw.WriteLine(lstQuestions[i].blDIsCorrect);

// True Or False variables...
sw.WriteLine(lstQuestions[i].blTrueOrFalseAnswer);

// Fill-In-The-Blank variables...
sw.WriteLine(lstQuestions[i].strFIBAnswer);

// Answer Response variables...
sw.WriteLine(lstQuestions[i].strIAR);
sw.WriteLine(lstQuestions[i].strIAR);
sw.WriteLine(lstQuestions[i].blIARImagePresent);
sw.WriteLine(lstQuestions[i].blCARAudioPresent);
sw.WriteLine(lstQuestions[i].blCARVideoPresent);
sw.WriteLine(lstQuestions[i].blIARImagePresent);
sw.WriteLine(lstQuestions[i].blIARAudioPresent);
sw.WriteLine(lstQuestions[i].blIARVideoPresent);
}
}
sw.Close();
}

//Now write the image...
if (GameInfo.blTSImagePresent == true)
{
var TSImage = archive.CreateEntry("imgTSImage");

using (var entryStream = TSImage.Open())
using (var sw = new StreamWriter(entryStream))
{
clsMediaConverter MC = new clsMediaConverter();
sw.Write(GameInfo.imgTSImage.ToString());
sw.Close();
}
}
}
using (var fileStream = new FileStream(strfilepath, FileMode.Create))
{
ms.Seek(0, SeekOrigin.Begin);
ms.CopyTo(fileStream);
}
}
}

问题:图像未保存。我在 7zip 中打开生成的 zip 文件,并尝试在网络浏览器中打开图像,但我得到的只是“System.Drawing.Image”。甚至可以通过这种方式将图像保存到 zip 存档中吗?如果没有,我怎么能不从硬盘上的原始位置写入图像文件呢?

最佳答案

您正在使用只写入文本的流编写器,您需要保存图像,而不是类名:

            //Now write the image...
if (GameInfo.blTSImagePresent == true)
{
var TSImage = archive.CreateEntry("imgTSImage");

using (var entryStream = TSImage.Open())
{
GameInfo.imgTSImage.Save(entryStream, ImageFormat.Jpeg);
}

}

关于C# 将图像写入 ziarchive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44447364/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com