Search code examples
c#openfiledialogsavefiledialog

saveFileDialog-Image: Select image and then save it in another folder


I'm creating an application and I want to create a label which when clicked, will open an "openFileDialog", the user will select a .jpg or .png image, and then this selected image will be copied to a predefined directory (specific folder).

For now, I have a separate label-button which when clicked, opens an "openFileDialog" and the selected picture is then shown in a pictureBox. What I do not know, is how to grab this selected picture, and copy it in the predfined directory.

I would also like to somehow rename it when copying it to the directory, so I can later short the pictures by date added and display them through another label-button.

Here is the code I have, for the very first button:

private void addlabel_MouseClick(object sender, MouseEventArgs e)
    {
        Image File;
        OpenFileDialog f = new OpenFileDialog();
        f.Filter = "Image files (*.jpg, *.png) | *.jpg; *.png";

        if (f.ShowDialog() == DialogResult.OK)
        {
            File = Image.FromFile(f.FileName);
            pictureBox3.Image = File;
        }
    }

Thanks in advance!


Solution

  • Try

    pictureBox3.Image.Save(specific_folder + "\\" + f.SafeFileName);
    

    as follow:

    private void addlabel_MouseClick(object sender, MouseEventArgs e)
    {
        Image File;
        OpenFileDialog f = new OpenFileDialog();
        f.Filter = "Image files (*.jpg, *.png) | *.jpg; *.png";
    
        if (f.ShowDialog() == DialogResult.OK)
        {
            File = Image.FromFile(f.FileName);
            pictureBox3.Image = File;
            pictureBox3.Image.Save(specific_folder + "\\" + f.SafeFileName);
        }
    }