Search code examples
c#winformspicturebox

I can't save a photo taken from pictureBox1 in the folder I specified


 var fd = new SaveFileDialog();
        fd.Filter = "Bmp(*.BMP;)|*.BMP;| Jpg(*Jpg)|*.jpg";
      
        if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            switch (Path.GetExtension(fd.FileName))
            {
                case ".BMP":
                    pictureBox1.Image.Save(fd.FileName, ImageFormat.Bmp);
                    break;
                case ".Jpg":
                    pictureBox1.Image.Save(fd.FileName, ImageFormat.Jpeg);
                    break;
                default:
                    break;
            }
        }

The function should save the picture with pictureBox in a file but the save window appears. I save but the file simply does not appear in the folder where I saved


Solution

  • The only obvious thing that I can see is that the switch statement might fall through.

    Add the .ToLower() when you check the extension and check lowercase extensions.

    ...
    ...
        switch (Path.GetExtension(fd.FileName).ToLower())
        {
            case ".bmp":
                pictureBox1.Image.Save(fd.FileName, ImageFormat.Bmp);
               break;
            case ".jpg":
                pictureBox1.Image.Save(fd.FileName, ImageFormat.Jpeg);
                break;
            default:
                break;
        }
    ...
    ...