title: Converting between image file types in c#
body: <p>On my website, I'm receiving image files for upload. I accept files of type jpg, png, bmp or gif. If the user uploads a jpg, png or gif I want to save the file keeping the file type, but if the user uploads a bmp I want to convert it to a png.</p>
  
  <p>I have a method stub like this:</p>
  
  <pre><code>private void profileImgUpload(HttpPostedFile profileImg)
  </code></pre>
  
  <p><code>HttpPostedFile</code> has a method <code>saveAs</code>, to save the file, or else a property <code>InputStream</code> to get a stream to work with.</p>
  
  <p>In the previous implementation, someone had written this:</p>
  
  <pre><code>String[] allowedExtensions = { ".png", ".jpeg", ".jpg", ".gif" };
  for (int i = 0; i &lt; allowedExtensions.Length; i++)
      {
          if (FileExtension == allowedExtensions[i])
              FileOK = true;
      }
  }
  
  if (FileOK)
      profileImg.SaveAs(physicalPath + "newAvatarTemp.png");
  </code></pre>
  
  <p>I have assumed that this just results in the file being saved with the name <code>newAvatarTemp.png</code>, but the actual encoding type is not changed.</p>