質問

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

役に立ちましたか?

解決

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();
}

他のヒント

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.

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);

You write code like this.

string appPath = Path.GetDirectoryName(Application.ExecutablePath) +foldername ;
pictureBox1.Image.Save(appPath + @"\" + filename + ".jpg", ImageFormat.Jpeg);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top