Issue while saving image using savefiledialog

Posted by user1097772 on Stack Overflow See other posts from Stack Overflow or by user1097772
Published on 2012-06-15T15:09:38Z Indexed on 2012/06/15 15:16 UTC
Read the original article Hit count: 266

Filed under:
|
|
|
|

I'm using savefiledialog to save an image. Canvas is picturebox and the loaded image is bitmap. When I try to save it the file is created but somehow corrupted. Cause when I try againt load the image or show in different viewer it doesn't work - I mean the saved file is corrupted. There is an method for saving image.

 private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

           System.IO.FileStream fs =
                (System.IO.FileStream)saveFileDialog1.OpenFile();

           try
           {
               switch (saveFileDialog1.FilterIndex)
               {
                   case 1:
                       canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
                       break;
                   case 2:
                       canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                       break;
                   case 3:
                       canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
                       break;
                   case 4:
                       canvas.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Tiff);
                       break;
               }

           }
           catch (Exception ex) 
           {
               System.Console.WriteLine("Exception " + ex);
           }

I should also mention the property Filter. saveFileDialog1.Filter has value:

bmp (*.bmp)|*.bmp|jpeg (*.jpeg)|*.jpeg|png (*.png)|*.png|tiff (*.tiff)|*.tiff

© Stack Overflow or respective owner

Related posts about c#

Related posts about image