title: Creating an Sprite Sheet with C#, does not save
body: <p>Okay so I'm making a spritesheet maker at the moment and I have hit a little snag.<br>
  I have made the code and it successfully pulls all the images into the memory stream and places them together, but when it comes to saving the program returns a <code>Generic GDI+ error</code>.<br>
  I have tried opening another memory stream to save the new bitmap under but it turns out I cannot create a Memory Stream of a virtual file.<br>
  Anyway here's the code so far:<br></p>
  
  <pre><code>class SpriteMaker
  {
      static void Main()
      {
          bool cont = false;
          bool skip = false;
          while (cont == false)
          {
              Console.WriteLine("Enter a Folder name(end to end):");
              string fold = Console.ReadLine();
              if (fold.Equals("end"))
              {
                  break;
              }
              try
              {
                  string[] files = Directory.GetFiles(@"sprites\" + fold, "*.PNG");
                  foreach (string file in files)
                  {
                      Console.WriteLine(file);
                  }
                  skip = false;
              }
              catch (Exception)
              {
                  Console.WriteLine("Folder not Found!");
                  Console.WriteLine("Try Again!");
                  cont = false;
                  skip = true;
              }
              if (skip != true)
              {
                  try
                  {
                      string[] files = Directory.GetFiles(@"sprites\" + fold, "*.PNG");
                      System.Drawing.Bitmap stitchedImage = Combine(files);
                      Console.WriteLine("save filename (no extention)");
                      string fil = Console.ReadLine();
                      stitchedImage.Save(@"/sheets/"+fil+".png", System.Drawing.Imaging.ImageFormat.Png);
                      cont = true;
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine("Error Creating Sprite Sheet");
                      Console.WriteLine(ex);
                      Console.WriteLine("Please Try Again!");
                  }
              }
          }
          Console.WriteLine("Done!");
          Console.WriteLine("Program will now exit(Enter to continue)");
          Console.ReadLine();
      }
      public static System.Drawing.Bitmap Combine(string[] files)
      {
          //read all images into memory
          List&lt;System.Drawing.Bitmap&gt; images = new List&lt;System.Drawing.Bitmap&gt;();
          System.Drawing.Bitmap finalImage = null;
  
          try
          {
              int width = 0;
              int height = 0;
  
              foreach (string image in files)
              {
                  //create a Bitmap from the file and add it to the list
                  System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);
  
                  //update the size of the final bitmap
                  width += bitmap.Width;
                  height = bitmap.Height &gt; height ? bitmap.Height : height;
  
                  images.Add(bitmap);
              }
  
              //create a bitmap to hold the combined image
              finalImage = new System.Drawing.Bitmap(width, height);
  
              //get a graphics object from the image so we can draw on it
              using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
              {
                  //set background color
                  g.Clear(System.Drawing.Color.Black);
  
                  //go through each image and draw it on the final image
                  int offset = 0;
                  foreach (System.Drawing.Bitmap image in images)
                  {
                      g.DrawImage(image,
                        new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
                      offset += image.Width;
                  }
              }
  
              return finalImage;
          }
          catch (Exception ex)
          {
              if (finalImage != null)
              {
                  finalImage.Dispose();
              }
              Console.WriteLine(ex);
              throw ex;
              Console.WriteLine("Press Enter to continue...");
              Console.ReadLine();
          }
          finally
          {
              //clean up memory
              foreach (System.Drawing.Bitmap image in images)
              {
                  image.Dispose();
              }
          }
      }
  }
  </code></pre>
  
  <p>Any help will be appreciated<br>
  EDIT: I have found the cause for this error it was to do with creating stitchedImage... however now I get a Memory Exception... then again loading 350 odd images into the MemoryStream is fatal :/</p>