0
 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

fubo
  • 44,811
  • 17
  • 103
  • 137
  • Are you seeing any exceptions being raised? Do you have write-access to the folder? Does the file get saved anywhere, just not where you expected? – Eight-Bit Guru Sep 22 '20 at 10:55
  • If i choose Bmp This write-access.System.NullReferenceException: "The object reference does not point to an object instance." For some reason, nowhere is it saved. – Super Lokiner Sep 22 '20 at 11:01
  • Your best friend, the [debugger](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx), should help! – TaW Sep 22 '20 at 15:29

1 Answers1

2

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;
    }
...
...
CobyC
  • 2,058
  • 1
  • 18
  • 24
  • System.NullReferenceException: "The object reference does not point to an object instance." This error appeared. – Super Lokiner Sep 22 '20 at 11:05
  • Does your picture box show the image? if there is not image in the picture box it will throw that exception. – CobyC Sep 22 '20 at 11:14
  • Yes indeed. The problem was that although I painted in PicturBox, I did not insert the original image there. Thank you. – Super Lokiner Sep 22 '20 at 17:34