How can I save Image files in in my project folder?
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);
Comments
-
Amit Kumar about 4 years
In my Windows application, I have a
PictureBox
and aButton
control. I want to load an Image file from user from the button'sOnClick
event and save that image file in a folder name "proImg" which is in my project. Then I want to show that image in thePictureBox
.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.
-
Amit Kumar over 10 yearsstill same type of exception .
-
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 over 10 yearsAfter running please check your /bin folder to see the result.
-
Amit Kumar over 10 yearsbut images is not in my ProImages folder. it was in bin/ProImages folder .
-
Saleh Parsa over 10 yearsIt is going to a PRoImages folder in your /bin/Debug folder aka (executable file directory)
-
Amit Kumar over 10 yearswhat if i want to save this path to database, so that later i can use this path to display image.
-
Saleh Parsa over 10 yearsYou may create one more
string
asstring dbPath = appPath + iName;
and then store dbPath to your database because it will be contains both directory and file name -
Waruna Manjula almost 8 yearsusing System.Drawing.Imaging;
-
paraJdox1 about 3 yearshow 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.