title: Convert All to JPG in C#
body: <p>I have a function like the following:</p>
  
  <pre><code>    // Convert To JPG 
      // 
      public string AlltoJPG(FileInfo foo)
      {
          // Get file extension
          string fileExtension = foo.Extension;
  
          // Get file name without extenstion
          string fileName = foo.Name.Replace(foo.Extension, string.Empty) + ".jpg";
  
          /*------------------------------------------------------------------------*/
  
          /// &lt;Check for PNG File Format&gt;
          if (fileExtension == ".png" || fileExtension == ".PNG")
          {
              System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName);
  
              // Assumes img is the PNG you are converting
              using (Bitmap b = new Bitmap(img.Width, img.Height))
              {
                  using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b))
                  {
                      g.Clear(System.Drawing.Color.White);
                      g.DrawImageUnscaled(img, 0, 0);
                  }
  
                  // Save the image as a JPG
                  b.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
              }
  
          }
  
          /*------------------------------------------------------------------------*/
  
          /// &lt;Check for GIF File Format&gt;
          if (fileExtension == ".gif" || fileExtension == ".GIF")
          {
              System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName);
  
              // Construct a bitmap from the image resource.
              Bitmap bmp1 = new Bitmap(img.Width, img.Height);
  
              // Save the image as a JPG
              bmp1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
          }
  
          /*------------------------------------------------------------------------*/
  
          /// &lt;Check for BMP File Format&gt;
          if (fileExtension == ".bmp" || fileExtension == ".BMP")
          {
              System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName);
  
              // Construct a bitmap from the image resource.
              Bitmap bmp1 = new Bitmap(img.Width, img.Height);
  
              // Save the image as a JPG
              bmp1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
          }
  
          /*------------------------------------------------------------------------*/
  
          /// &lt;Check for TIFF File Format&gt;
          if (fileExtension == ".tiff" || fileExtension == ".TIFF")
          {
              System.Drawing.Image img = System.Drawing.Image.FromFile(foo.FullName);
  
              // Construct a bitmap from the image resource.
              Bitmap bmp1 = new Bitmap(img.Width, img.Height);
  
              // Save the image as a JPG
              bmp1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
          }
  
          /*------------------------------------------------------------------------*/
          fileName = foo.DirectoryName + "\\" + fileName;
          return fileName;
      }
  </code></pre>
  
  <p>I'm trying to convert <code>bmp</code>,<code>png</code>,<code>gif</code>,<code>tiff</code> file formats to <code>jpg</code>, but <code>GDI+</code> as gives: </p>
  
  <blockquote>
    <p>System.OutOfMemoryException was unhandled<br>
          Message=Bellek yetersiz.<br>
          Source=System.Drawing<br>
          StackTrace:<br>
               konum: System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)<br>
               konum: System.Drawing.Image.FromFile(String filename)</p>
  </blockquote>
  
  <p>So how can I avoid this and convert at least <code>PNG</code> and <code>BMP</code> files to <code>JPG</code>?</p>
  
  <p>Here is a picture of the error:</p>
  
  <p><img src="https://i.stack.imgur.com/TRugQ.jpg" alt="enter image description here"></p>