I would like to be able to save image according to extension that is entered in the save file dialog. I have found out that simply entering e.g. "JPG" does not cause the Save method to use this format of course. Parsing the extension and then using e.g. switch and setting correct format sounds a bit ackward to me. Or there is no better way?
Click the File menu and click Save as. 3. In the File name text box, type "filename. extension" and click Save.
To save a file using the SaveFileDialog component. Display the Save File dialog box and call a method to save the file selected by the user. Use the SaveFileDialog component's OpenFile method to save the file. This method gives you a Stream object you can write to.
what is a . save file? Files with the . save extension are temporary files containing data associated with text documents created using the Unix Nano text editing application. These SAVE files are created when this text editing application has limited memory resources at its disposal.
Files with . cs extension are source code files for C# programming language.
you can get the file name specified in the SaveDialog.FileName
then with Path.GetExtension()
or similar you can get the string which will be used as extension.
What you will do after depends on your specific application design, if you are saving a text file you can also call it image1.png, but it will still be a text file.
if you have an image object in memory and want to save in the proper format depending on the selected extension, I would use a switch/case and use the proper overload or parameter values of the Image.Save to handle the different image formats.
if(DialogResult.OK == saveDialog.ShowDialog())
{
var extension = Path.GetExtension(saveDialog.FileName);
switch(extension.ToLower())
{
case ".jpg":
// ToDo: Save as JPEG
break;
case ".png":
// ToDo: Save as PNG
break;
default:
throw new ArgumentOutOfRangeException(extension);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With