C# 将图像写入 ziarchive

标签 c# .net image ziparchive system.io.compression

我正在编写一个 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/

相关文章:

c++ - 有什么地方可以让我查看 .NET 秒表代码并了解它在做什么?

CSS 和图像 - 为什么图片没有调整大小

javascript - ASP.NET URL 编码不带 ?或变量名

c# - 你能使 VB.NET 编译像 C# 一样严格吗?

.net - 为什么我的构建服务器中的部署包有额外的程序集?

.net - 检测整个窗口的箭头键-KeyDown

c# - 未聚焦窗口上的控件不会影响首次单击

C#异步等待实现

PHP - 显示数据库中的图像

python - 从图像 RGB 值列表中删除 [255,255,255] 条目