How can I save Image files in in my project folder?

47,667

Solution 1

Actually the string iName = opFile.FileName; is not giving you the full path. You must use the SafeFileName instead. I assumed that you want your folder on your exe directory also. Please refer to my modifications:

OpenFileDialog opFile = new OpenFileDialog();
opFile.Title = "Select a Image";
opFile.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";

string appPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\ProImages\"; // <---
if (Directory.Exists(appPath) == false)                                              // <---
{                                                                                    // <---
    Directory.CreateDirectory(appPath);                                              // <---
}                                                                                    // <---

if (opFile.ShowDialog() == DialogResult.OK)
{
    try
    {
        string iName = opFile.SafeFileName;   // <---
        string filepath = opFile.FileName;    // <---
        File.Copy(filepath, appPath + iName); // <---
        picProduct.Image = new Bitmap(opFile.OpenFile());
    }
    catch (Exception exp)
    {
        MessageBox.Show("Unable to open file " + exp.Message);
    }
}
else
{
    opFile.Dispose();
}

Solution 2

You should provide a correct destination Path to File.Copy method. "~\ProImages..." is not a correct path. This example will copy selected picture to folder ProImages inside the project's bin folder :

string iName = opFile.FileName;
File.Copy(iName, Path.Combine(@"ProImages\", Path.GetFileName(iName)));

the path is relative to current executable file's location, except you provide full path (i.e. @"D:\ProImages").

In case you didn't create the folder manually and want the program generate ProImages folder if it doesn't exist yet :

string iName = opFile.FileName;
string folder = @"ProImages\";
var path = Path.Combine(folder, Path.GetFileName(iName))
if (!Directory.Exists(folder))
{
    Directory.CreateDirectory(folder);
}
File.Copy(iName, path);

PS: Notice the use of verbatim (@) to automatically escape backslash (\) characters in string. It is common practice to use verbatim when declaring string that represent path.

Solution 3

Try using picturebox.Image.Save function. In my program it is working PictureBox.Image.Save(Your directory, ImageFormat.Jpeg)

example pictureBox2.Image.Save(@"D:/CameraImge/"+foldername+"/"+ numbering + ".jpg", ImageFormat.Jpeg);

Solution 4

You write code like this.

string appPath = Path.GetDirectoryName(Application.ExecutablePath) +foldername ;
pictureBox1.Image.Save(appPath + @"\" + filename + ".jpg", ImageFormat.Jpeg);
Share:
47,667
Amit Kumar
Author by

Amit Kumar

There's only one religion, StackOverflow.

Updated on April 08, 2020

Comments

  • Amit Kumar
    Amit Kumar about 4 years

    In my Windows application, I have a PictureBox and a Button control. I want to load an Image file from user from the button's OnClick event and save that image file in a folder name "proImg" which is in my project. Then I want to show that image in the PictureBox.

    I have written this code, but it is not working:

    OpenFileDialog opFile = new OpenFileDialog();
    opFile.Title = "Select a Image";
    opFile.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*";
    if (opFile.ShowDialog() == DialogResult.OK)
    {
        try
        {
            string iName = opFile.FileName;
            string filepath = "~/images/" + opFile.FileName;
            File.Copy(iName,Path.Combine("~\\ProImages\\", Path.GetFileName(iName)));
            picProduct.Image = new Bitmap(opFile.OpenFile());
        }
        catch (Exception exp)
        {
            MessageBox.Show("Unable to open file " + exp.Message);
        }
    }
    else
    {
        opFile.Dispose();
    }
    

    It's not able to save the image in the "proImg" folder.

    enter image description here

  • Amit Kumar
    Amit Kumar over 10 years
    still same type of exception .
  • har07
    har07 over 10 years
    File.Copy assume the folder is already exist. If it isn't you need to create the folder first. Is that the case, ProImages folder hasn't been created yet?
  • Saleh Parsa
    Saleh Parsa over 10 years
    After running please check your /bin folder to see the result.
  • Amit Kumar
    Amit Kumar over 10 years
    but images is not in my ProImages folder. it was in bin/ProImages folder .
  • Saleh Parsa
    Saleh Parsa over 10 years
    It is going to a PRoImages folder in your /bin/Debug folder aka (executable file directory)
  • Amit Kumar
    Amit Kumar over 10 years
    what if i want to save this path to database, so that later i can use this path to display image.
  • Saleh Parsa
    Saleh Parsa over 10 years
    You may create one more string as string dbPath = appPath + iName; and then store dbPath to your database because it will be contains both directory and file name
  • Waruna Manjula
    Waruna Manjula almost 8 years
    using System.Drawing.Imaging;
  • paraJdox1
    paraJdox1 about 3 years
    how to do this in separate button? the other button (btnBrowse) loads the image in the picture box, the other button (btnSave) copies the image to the relative folder.