C# -Savefile Dialog to save an image file
-
hi friends I am new to .NET, I have the following code to save an image file in specific location. It does compile and it is showing that null reference eexception is unhandled it is asking to create an object instance in this line (System.Drawing.Imaging.ImageFormat) how can it be done any help please SaveFileDialog save = new SaveFileDialog(); save.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; save.Title = "Save the File"; save.ShowDialog(); string fName = save.FileName; if (save.FileName != "") { System.IO.Stream fileStream = (System.IO.FileStream)save.OpenFile(); switch (save.FilterIndex) { case 1: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; case 2: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Gif); break; case 3: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Bmp); break; } fileStream.Close(); } }
-
hi friends I am new to .NET, I have the following code to save an image file in specific location. It does compile and it is showing that null reference eexception is unhandled it is asking to create an object instance in this line (System.Drawing.Imaging.ImageFormat) how can it be done any help please SaveFileDialog save = new SaveFileDialog(); save.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; save.Title = "Save the File"; save.ShowDialog(); string fName = save.FileName; if (save.FileName != "") { System.IO.Stream fileStream = (System.IO.FileStream)save.OpenFile(); switch (save.FilterIndex) { case 1: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; case 2: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Gif); break; case 3: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Bmp); break; } fileStream.Close(); } }
I think the error is here:
vijaywithu wrote:
System.IO.Stream fileStream = (System.IO.FileStream)save.OpenFile();
For whatever reason, the OpenFile has returned a null Stream (Did you press cancel?)
-
hi friends I am new to .NET, I have the following code to save an image file in specific location. It does compile and it is showing that null reference eexception is unhandled it is asking to create an object instance in this line (System.Drawing.Imaging.ImageFormat) how can it be done any help please SaveFileDialog save = new SaveFileDialog(); save.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; save.Title = "Save the File"; save.ShowDialog(); string fName = save.FileName; if (save.FileName != "") { System.IO.Stream fileStream = (System.IO.FileStream)save.OpenFile(); switch (save.FilterIndex) { case 1: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; case 2: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Gif); break; case 3: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Bmp); break; } fileStream.Close(); } }
Two things: 1: SaveFileDialog save = new SaveFileDialog(); save.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"; save.Title = "Save the File"; if(save.ShowDialog() == DialogResult.OK) Use this to prevent exception on Cancel click { string fName = save.FileName; if (save.FileName != "") { System.IO.Stream fileStream = (System.IO.FileStream)save.OpenFile(); switch (save.FilterIndex) { case 1: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; case 2: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Gif); break; case 3: this.button7.Image.Save(fileStream, System.Drawing.Imaging.ImageFormat.Bmp); break; } fileStream.Close(); } } } 2: Set Buttons Image Property !!!