title: Why does IrfanView say my jpg file is a png file?
body: <p>My utility is supposed to resize either .jpg or .png files.</p>
  
  <p>It seems to work fine in one location (at work, where I don't have IrfanView installed). But at home, when I open a *.jpg and then save it (resized), I see:</p>
  
  <p><img src="https://i.stack.imgur.com/wARsx.png" alt="enter image description here"></p>
  
  <p>However, the image still displays fine in either case (whether I select "Yes" or "No" in the dialog.</p>
  
  <p>IOW, I'm able to load and save both jpgs and pngs, and they save as such, and display fine. But IrfanView claims they are messed up.</p>
  
  <p>Actually, I'm not sure how the image is saved; I was assuming it just saved it in the proper format "behind the scenes" based on the extension. Anyway, as this is a rather simple utility, I will just show all the code:</p>
  
  <pre><code>using System;
  using System.Drawing;
  using System.IO;
  using System.Windows.Forms;
  
  namespace FileResizingUtil
  {
      public partial class FormFileResizer : Form
      {
          private Image _imgToResize;
          String _originalFilename = String.Empty;
  
          public FormFileResizer()
          {
              InitializeComponent();
          }
  
          private void buttonChooseImage_Click(object sender, EventArgs e)
          {
              OpenFileDialog ofd = new OpenFileDialog
              {
                  InitialDirectory = "c:\\",
                  Filter = "JPG files (*.jpg)|*.jpg| PNG files (*.png)|*.png", FilterIndex = 2, RestoreDirectory = true
              };
  
  
              if (ofd.ShowDialog() == DialogResult.OK)
              {
                  try
                  {
                      _originalFilename = ofd.FileName;
                      _imgToResize = Image.FromFile(_originalFilename);
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                  }
              }
              // If made it to here, it must be good
              String preamble = labelImgSelected.Text;
              labelImgSelected.Text = String.Format("{0}{1}", preamble, _originalFilename);
              textBoxOrigHeight.Text = _imgToResize.Height.ToString();
              textBoxOrigWidth.Text = _imgToResize.Width.ToString();
              buttonApplyPercentageChange.Enabled = true;
              //buttonResizeImage.Enabled = true;
          }
  
          private void buttonResizeImage_Click(object sender, EventArgs e)
          {
              // Really large images take awhile, so show an hourglass
              Cursor.Current = Cursors.WaitCursor;
              try
              {
                  var size = new Size { Height = Convert.ToInt32(textBoxNewHeight.Text), Width = int.Parse(textBoxNewWidth.Text) };
                  // Two different ways of getting the int val
                  Image resizedImg = FileResizeUtils.GetResizedImage(_imgToResize, size);
  
                  String fileNameSansExtension = Path.GetFileNameWithoutExtension(_originalFilename);
                  String fileNameExtension = Path.GetExtension(_originalFilename);
                  String newFilename = String.Format("{0}{1}_{2}{3}", fileNameSansExtension, size.Height, size.Width, fileNameExtension);
                  // If used a different extension (jpg where the original was png, or vice versa) would the Save be intelligent enough to actually save in the other format?
                  resizedImg.Save(newFilename);
  
                  MessageBox.Show(String.Format("Done! File saved as {0}", newFilename));
                  Recycle();
              }
              finally
              {
  
                  Cursor.Current = Cursors.Default;
              }
          }
  
          private void Recycle()
          {
              buttonResizeImage.Enabled = false;
              buttonApplyPercentageChange.Enabled = false;
              labelImgSelected.Text = "Image selected: ";
              textBoxOrigHeight.Text = String.Empty;
              textBoxOrigWidth.Text = String.Empty;
              // Retain the percentage vals, as it may be that all in a batch need to be the same pair of vals
          }
  
          private void buttonApplyPercentageChange_Click(object sender, EventArgs e)
          {
              int origHeight = _imgToResize.Height;
              int origWidth = _imgToResize.Width;
  
              // Two ways to convert the val
              double heightFactor = (double)numericUpDownHeight.Value / 100.0;
              double widthFactor = Convert.ToDouble(numericUpDownWidth.Value) / 100.0;
              if (heightFactor &lt; 0 || widthFactor &lt; 0)
              {
                  // show an error - no negative values allowed- using updown, so that should not be possible
              }
              var newHeight = Convert.ToInt32(origHeight * heightFactor);
              var newWidth = Convert.ToInt32(origWidth * widthFactor);
              textBoxNewHeight.Text = newHeight.ToString();
              textBoxNewWidth.Text = newWidth.ToString();
              buttonResizeImage.Enabled = true;
          }
  
          private void textBoxNewHeight_TextChanged(object sender, EventArgs e)
          {
              EnableResizeButtonIfValidDimensionsEntered();
          }
  
          private void EnableResizeButtonIfValidDimensionsEntered()
          {
              if (String.IsNullOrWhiteSpace(textBoxOrigHeight.Text)) return;
              String candidateHeight = textBoxNewHeight.Text;
              String candidateWidth = textBoxNewWidth.Text;
              int validHeight;
              int validWidth;
              buttonResizeImage.Enabled = (int.TryParse(candidateHeight, out validHeight)) &amp;&amp;
                                          (int.TryParse(candidateWidth, out validWidth));
          }
  
          private void numericUpDownHeight_ValueChanged(object sender, EventArgs e)
          {
              if (checkBoxRetainRatio.Checked)
              {
                  numericUpDownWidth.Value = numericUpDownHeight.Value;
              }
          }
  
          private void numericUpDownWidth_ValueChanged(object sender, EventArgs e)
          {
              if (checkBoxRetainRatio.Checked)
              {
                  numericUpDownHeight.Value = numericUpDownWidth.Value;
              }
          }
  
      }
  }
  </code></pre>
  
  <p>..and the GUI (just prior to hitting the "Resize Image" button:</p>
  
  <p><img src="https://i.stack.imgur.com/PsYd8.png" alt="enter image description here"></p>
  
  <h2>UPDATE</h2>
  
  <p>Based on Eugene Sh.'ls comment, I changed my Save method to the following block:</p>
  
  <pre><code>bool success = true;
  . . .
  if (fileNameExtension != null &amp;&amp; fileNameExtension.ToLower().Contains("jpg"))
  {
      resizedImg.Save(newFilename, ImageFormat.Jpeg);
  }
  else if (fileNameExtension != null &amp;&amp; 
           fileNameExtension.ToLower().Contains("png"))
  {
      resizedImg.Save(newFilename, ImageFormat.Png);
  }
  else
  {
      success = false;
  }
  
  if (success)
  {
      MessageBox.Show(String.Format("Done! File saved as {0}", newFilename));
  }
  else
  {
      MessageBox.Show("Something went awry. The file was not saved");
  }
  </code></pre>
  
  <h2>UPDATE 2</h2>
  
  <p>So here is my new code, implementing the suggestion, and supporting several new file types:</p>
  
  <pre><code>using System;
  using System.Drawing;
  using System.Drawing.Imaging;
  using System.IO;
  using System.Windows.Forms;
  
  namespace FileResizingUtil
  {
      public partial class FormFileResizer : Form
      {
          private Image _imgToResize;
          String _originalFilename = String.Empty;
  
          public FormFileResizer()
          {
              InitializeComponent();
          }
  
          private void buttonChooseImage_Click(object sender, EventArgs e)
          {
              OpenFileDialog ofd = new OpenFileDialog
              {
                  InitialDirectory = "c:\\",
                  Filter = "JPG files (*.jpg)|*.jpg| PNG files (*.png)|*.png| BMP files (*.bmp)|*.bmp| TIFF files (*.tiff)|*.png| ICO files (*.ico)|*.ico| EMF files (*.emf)|*.emf| WMF files (*.wmf)|*.wmf",
                      FilterIndex = 1, RestoreDirectory = true
              };
  
  
              if (ofd.ShowDialog() == DialogResult.OK)
              {
                  try
                  {
                      _originalFilename = ofd.FileName;
                      _imgToResize = Image.FromFile(_originalFilename);
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                  }
              }
              if (String.IsNullOrWhiteSpace(_originalFilename)) return;
              // If made it to here, it must be good
              String preamble = labelImgSelected.Text;
              labelImgSelected.Text = String.Format("{0}{1}", preamble, _originalFilename);
              textBoxOrigHeight.Text = _imgToResize.Height.ToString();
              textBoxOrigWidth.Text = _imgToResize.Width.ToString();
              buttonApplyPercentageChange.Enabled = true;
          }
  
          private void buttonResizeImage_Click(object sender, EventArgs e)
          {
              bool success = true;
              // Really large images take awhile, so show an hourglass
              Cursor.Current = Cursors.WaitCursor;
              try
              {
                  // Two different ways of getting the int val
                  var size = new Size { Height = Convert.ToInt32(textBoxNewHeight.Text), Width = int.Parse(textBoxNewWidth.Text) };
                  Image resizedImg = FileResizeUtils.GetResizedImage(_imgToResize, size);
  
                  String fileNameSansExtension = Path.GetFileNameWithoutExtension(_originalFilename);
                  String fileNameExtension = Path.GetExtension(_originalFilename);
                  String newFilename = String.Format("{0}{1}_{2}{3}", fileNameSansExtension, size.Height, size.Width, fileNameExtension);
                  if (fileNameExtension != null &amp;&amp; fileNameExtension.ToLower().Contains("jpg"))
                  {
                      resizedImg.Save(newFilename, ImageFormat.Jpeg);
                  }
                  else if (fileNameExtension != null &amp;&amp; fileNameExtension.ToLower().Contains("png"))
                  {
                      resizedImg.Save(newFilename, ImageFormat.Png);
                  }
                  else if (fileNameExtension != null &amp;&amp; fileNameExtension.ToLower().Contains("bmp"))
                  {
                      resizedImg.Save(newFilename, ImageFormat.Bmp);
                  }
                  else if (fileNameExtension != null &amp;&amp; fileNameExtension.ToLower().Contains("ico"))
                  {
                      resizedImg.Save(newFilename, ImageFormat.Icon);
                  }
                  else if (fileNameExtension != null &amp;&amp; fileNameExtension.ToLower().Contains("tiff"))
                  {
                      resizedImg.Save(newFilename, ImageFormat.Tiff);
                  }
                  else if (fileNameExtension != null &amp;&amp; fileNameExtension.ToLower().Contains("emf"))
                  {
                      resizedImg.Save(newFilename, ImageFormat.Emf);
                  }
                  else if (fileNameExtension != null &amp;&amp; fileNameExtension.ToLower().Contains("wmf"))
                  {
                      resizedImg.Save(newFilename, ImageFormat.Wmf);
                  }
                  else
                  {
                      success = false;
                  }
  
                  MessageBox.Show(success
                      ? String.Format("Done! File saved as {0}", newFilename)
                      : "Something went awry. The file was not saved");
                  Recycle();
              }
              finally
              {
  
                  Cursor.Current = Cursors.Default;
              }
          }
  
          private void Recycle()
          {
              buttonResizeImage.Enabled = false;
              buttonApplyPercentageChange.Enabled = false;
              labelImgSelected.Text = "Image selected: ";
              textBoxOrigHeight.Text = String.Empty;
              textBoxOrigWidth.Text = String.Empty;
              // Retain the percentage vals, as it may be that all in a batch need to be the same pair of vals
          }
  
          private void buttonApplyPercentageChange_Click(object sender, EventArgs e)
          {
              int origHeight = _imgToResize.Height;
              int origWidth = _imgToResize.Width;
  
              // Two ways to convert the val
              double heightFactor = (double)numericUpDownHeight.Value / 100.0;
              double widthFactor = Convert.ToDouble(numericUpDownWidth.Value) / 100.0;
              if (heightFactor &lt; 0 || widthFactor &lt; 0)
              {
                  // show an error - no negative values allowed- using updown, so that should not be possible
              }
              var newHeight = Convert.ToInt32(origHeight * heightFactor);
              var newWidth = Convert.ToInt32(origWidth * widthFactor);
              textBoxNewHeight.Text = newHeight.ToString();
              textBoxNewWidth.Text = newWidth.ToString();
              buttonResizeImage.Enabled = true;
          }
  
          private void textBoxNewHeight_TextChanged(object sender, EventArgs e)
          {
              EnableResizeButtonIfValidDimensionsEntered();
          }
  
          private void EnableResizeButtonIfValidDimensionsEntered()
          {
              if (String.IsNullOrWhiteSpace(textBoxOrigHeight.Text)) return;
              String candidateHeight = textBoxNewHeight.Text;
              String candidateWidth = textBoxNewWidth.Text;
              int validHeight;
              int validWidth;
              buttonResizeImage.Enabled = (int.TryParse(candidateHeight, out validHeight)) &amp;&amp;
                                          (int.TryParse(candidateWidth, out validWidth));
          }
  
          private void numericUpDownHeight_ValueChanged(object sender, EventArgs e)
          {
              if (checkBoxRetainRatio.Checked)
              {
                  numericUpDownWidth.Value = numericUpDownHeight.Value;
              }
          }
  
          private void numericUpDownWidth_ValueChanged(object sender, EventArgs e)
          {
              if (checkBoxRetainRatio.Checked)
              {
                  numericUpDownHeight.Value = numericUpDownWidth.Value;
              }
          }
  
      }
  }
  </code></pre>