0

So, I am making a project in Windows Forms in which I have to change image formats. I found different solutions for that, such as: convert tiff to jpg format
c# convert image formats to jpg. And these solutions are working, but they all have one similarity:

image.Save(path,ImageFormat.Jpeg)

In this code I am saving the image in one directory. What I want to do is to save this formatted images with Savefiledialog. But in this case, when I call Savefiledialog, I need to write "image" name and format. If I don't write the format, it creates a simple file without extension. I want to automatically save the file in the specified format. So how to do that?

Community
  • 1
  • 1
dave1993
  • 7
  • 7
  • Are you expecting them to type "[filename].[filetype]" in the savefiledialog, or are you expecting them to use the type selector? Do you have a filter string? Add your SaveFileDialog code and clarify how you're wanting to determine file type. You're basically going to need to run a case statement on some criteria, and save it in different types based on what they select... But we can't help with what you've provided. – Aaron Feb 02 '16 at 18:08
  • This Code : image.Save(path,ImageFormat.Jpeg) means that this image would be saved in Directory "path" ? – dave1993 Feb 02 '16 at 18:19
  • `SaveFileDialog` will not give you the format - just the file name. It's up to you to pick out the extension from the dialog (once it returns a result), and save accordingly. – code4life Feb 02 '16 at 19:06

1 Answers1

0

I would write something simple like this:

// might make sense to make this a property under the relevant class
Dictionary<string, ImageFormat> mapOfExtensions = 
    new Dictionary<string, ImageFormat>
    {
        {".jpg", ImageFormat.Jpeg},
        {".jpeg", ImageFormat.Jpeg},
        {".png", ImageFormat.Png}
    };

var ext = Path.GetExtension(path);

// safety check - make sure the file extension is what we expect...
if (!mapOfExtensions.Contains(ext)) 
{
    // probably would make sense to throw an error, log a message, etc.
    return;
}

image.Save(path,mapOfExtensions[ext]);  
code4life
  • 15,655
  • 7
  • 50
  • 82
  • No no, I am can do that to. I want to save formated image file in Computer with FileDialog. for example I am opening FileDialog and choosing Desktop for directory where I want to save Image, than once again I Open FIleDialog and saving it in MyDocuments and s.o . I want easily save formated image in Computer. I donn't want to write everytime image.Save("C://Desktop",mapOfExtensions[ext]) or image.Save("D://Images",mapOfExtensions[ext]) . – dave1993 Feb 02 '16 at 20:27
  • I think you're missing the point of object oriented programming dave. You put that code into a function, and anywhere in your application you want to call it, you simply call the function. You don't have to write that line all over the place. If you want to pick a default folder, you should set a global string variable to string.empty, then check it in that function, if it's not set, show a save file dialog and set it, if it's not empty, use that folder. There's nothing wrong with this answer. – Aaron Feb 02 '16 at 23:11