3

I'm writing a C# .NET(v4.6.1) WinForms app that will save a file as a System.IO.Compression.ZipArchive. First, I open an image using a SaveFileDialog() method and then I assign it to variable of type System.Drawing.Image. Then I save the file, start with strings, integers and booleans going into a text file. Next, I try to save the image from the variable rather than from its original file path on the hard disk. Here's the code to save the file, text file followed by the 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);
             }
          }
       }

Problem: The image isn't saving. I open the resulting zip file in 7zip and try to open the image in a web browser and all I got was "System.Drawing.Image." Is it even possible to save an image to a zip archive this way? If not, how can I do it without writing the image file from its original location on the hard disk?

manicdrummer
  • 161
  • 3
  • 14
  • 1
    That is because `GameInfo.imgTSImage.ToString()` will return `System.Drawing.Image` as it is an image. You need to save the raw image to the stream – Nkosi Jun 09 '17 at 00:20
  • Thanks for pointing that out, Nkosi. It did return "System.Drawing.Image" and I forgot to remove "ToString()" before posting the code here. Gusman, will your code example actually write the image to the zip archive or do I still need to do something else to accomplish that? – manicdrummer Jun 09 '17 at 18:28

1 Answers1

3

You are using an streamwriter which writes only text, you need to save the image, not the class name:

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

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

            }
Gusman
  • 14,905
  • 2
  • 34
  • 50