2

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!

Bill M.
  • 151
  • 4
  • 15
  • Why do you need to *grab the selected image*? You have the filename (in `f.FileName`), which you clearly know because you use it. So you have the filename you need to copy the file whether you load the file into a picturebox or not. So do some research on *how to copy a file in C#*. You can rename it to whatever you want while copying. – Ken White May 31 '18 at 00:26
  • Possible duplicate of [How to copy a file to another path?](https://stackoverflow.com/questions/1979920/how-to-copy-a-file-to-another-path) – mjwills May 31 '18 at 00:35

2 Answers2

1

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);
    }
}
Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
  • Sorry for the late response! This will get the image from the pictureBox3 and save it to the specific_folder, right ? Not copy it from its original path, if I'm correct. One more thing, what does the f.SafeFileName do exactly? Thanks! – Bill M. May 31 '18 at 16:19
  • `SafeFileName` returns a string that only contains the file name for the selected file not full path. – Hossein Golshani May 31 '18 at 19:41
0

Something like

    if (f.ShowDialog() == DialogResult.OK)
    {
        File = Image.FromFile(f.FileName);
        pictureBox3.Image = File;
        File.Copy(f.FileName, @"C:\MyDestination\" + f.SafeFileName;
    }

See https://msdn.microsoft.com/en-us/library/c6cfw35a(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog(v=vs.110).aspx

Erik Levin
  • 47
  • 1
  • 4