title: Incrementing a filename if the file already exists VB.NET to C# conversion
body: <p>I am currently rewriting some software i created from VB.NET to C# due to additional image analysis functions and filters that are not possible in VB.NET. Part of the program automatically creates a new folder for the day if one does not already exist (that works fine) and it is then supposed to start saving images stored in the image buffer of the camera to file while incrementing the file name. I am not entirely sure if i am using the correct method here as i am trying to convert from VB.NET.</p>
  
  <pre><code>        private void tmAutoCap_Tick(object sender, EventArgs e)
      {
  
  
          if (dAltitudeAngle &lt; Convert.ToDouble(tbSolarAltSet.Text) &amp; cbDarkSubAC.Checked == true)
          {
  
              try
              {
                  ImageBuffer ImgBuffer = default(ImageBuffer);
                  int x = 0;
                  int y = 0;
                  int BytesPerLine = 0;
                  Cursor = Cursors.WaitCursor;
  
  
                  int i = 0;
                  string FileMask = (Global_Variables.NewDirectory + "\\AutoCap_{0}.bmp");
                  string Filename = "";
                  Filename = string.Format(FileMask, i);
  
                  while (System.IO.File.Exists(Filename))
                  {
                      i += 1;
                      Filename = string.Format(FileMask, i);
                  }
  
                  Bitmap Dark = Image.FromFile(tbDarkRoot.Text, true) as Bitmap;
                  ImgBuffer = IcImagingControl1.ImageBuffers[1];
                  // Calculate the count of bytes ber line using the color format and the
                  // pixels per line of the image buffer.
                  BytesPerLine = (ImgBuffer.BitsPerPixel / 8) * ImgBuffer.PixelPerLine - 1;
                  for (y = 0; y &lt;= ImgBuffer.Lines - 1; y++)
                  {
                      for (x = 0; x &lt;= BytesPerLine; x++)
                      {
                          if (Dark.GetPixel(x, y).R &lt;= ImgBuffer[x, y])
                          {
                              ImgBuffer[x, y] = Convert.ToByte(ImgBuffer[x, y] - Dark.GetPixel(x, y).R);
                          }
                          else
                          {
                              ImgBuffer[x, y] = 0;
                          }
                      }
                  }
  
                  ImgBuffer.SaveAsBitmap(Filename);
  
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.StackTrace);
                  System.IO.File.AppendAllText(Application.UserAppDataPath + "\\SkyCamErrorLog.txt", string.Format("{0}{1}{2}{3}", Environment.NewLine, DateTime.Now.ToString(), Environment.NewLine, ex.ToString()));
  
              }
  </code></pre>
  
  <p>I had expected this to look for files and if it found any then automatically increment from the highest file already there by use of the filemask but it always fails to create any file and the error message thrown out is:</p>
  
  <pre><code>TIS.Imaging.ICException: Unknown error occurred Base Library Error : Permission denied
  at TIS.Imaging.ImageBuffer.SaveImage(String filename)
  at TIS.Imaging.ImageBuffer.SaveAsBitmap(String filename)
  at Sky_Cam_Version_2.SkyCamForm.tmAutoCap_Tick(Object sender, EventArgs e)
  </code></pre>
  
  <p>For this reason i believe there is something wrong with the way i am trying to create the filename.</p>