2

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

     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 文件并尝试在 Web 浏览器中打开图像,我得到的只是“System.Drawing.Image”。甚至可以通过这种方式将图像保存到 zip 存档中吗?如果没有,如果不从硬盘上的原始位置写入图像文件,我该怎么做?

4

1 回答 1

2

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

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

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

            }
于 2017-06-09T00:21:22.850 回答