It's me again. Am writing this code that allows the user to select
an image which is saved into a folder. However, I want the selected
image file name to be saved also so that I can load it later into a
picture box called MemberPics. Please help me. I am able to select an
image but the filename is not the one which is save. Please below is
the code for selecting an image.
private void btnpics_Click(object sender, EventArgs e)
{
OpenFileDialog openFD = new OpenFileDialog();
//string chosen_file = "";
openFD.Title = "Insert an image ";
openFD.InitialDirectory = "c:";
openFD.FileName = "";
openFD.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png";
if (openFD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Operation cancelled !");
}
else
{
chosen_file = openFD.FileName;
MemberPics.Image = Image.FromFile(chosen_file);
MemberPics.Image.Save("d:\\Pictures\\chosen_file", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
below is the code i want to use to load the image later into another picture box called display
display.Image = Image.FromFile(@"d:\\Pictures\\chosen_file.jpg");
The problem is am not able to save the image with the FileName property of the openDialog so i find it difficult to load the image into the display picturebox. please help me with how to use the FileName property of openDialog so that i can load different images into the display later.
private void btnpics_Click(object sender, EventArgs e)
{
OpenFileDialog openFD = new OpenFileDialog();
//string chosen_file = "";
openFD.Title = "Insert an image ";
openFD.InitialDirectory = "c:";
openFD.FileName = "";
openFD.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png";
if (openFD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Operation cancelled !");
}
else
{
chosen_file = openFD.FileName;
MemberPics.Image = Image.FromFile(chosen_file);
MemberPics.Image.Save("d:\\Pictures\\chosen_file", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
below is the code i want to use to load the image later into another picture box called display
display.Image = Image.FromFile(@"d:\\Pictures\\chosen_file.jpg");
The problem is am not able to save the image with the FileName property of the openDialog so i find it difficult to load the image into the display picturebox. please help me with how to use the FileName property of openDialog so that i can load different images into the display later.

Kirtan PatelRank #292 · 8.6K reputationPosted Oct 20, 2009, 8:47 AM EDT
Here is your Corrected Code ..Now it will Show Picture and also save it in Pictures Folder
friend, please check "do you like this answer" if it helped you :)
using System.IO;
private void btnpics_Click(object sender, EventArgs e)
{
OpenFileDialog openFD = new OpenFileDialog();
string chosen_file = "";
openFD.Title = "Insert an image ";
openFD.InitialDirectory = "c:";
openFD.FileName = "";
openFD.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png";
if (openFD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Operation cancelled !");
}
else
{
chosen_file = Path.GetFileName(openFD.FileName);
MemberPics.Image = Image.FromFile(openFD.FileName);
MemberPics.Image.Save(@"D:\Pictures\"+chosen_file);
}
Dushan Stankovic72 reputationPosted Oct 20, 2009, 9:18 AM EDT
JONAS BOATENG24 reputationPosted Oct 20, 2009, 10:00 AM EDT
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Oct 20, 2009, 9:12 PM EDT
private void btnpics_Click(object sender, EventArgs e)
{
OpenFileDialog openFD = new OpenFileDialog();
//string chosen_file = "";
openFD.Title = "Insert an image ";
openFD.InitialDirectory = "c:";
openFD.FileName = "";
openFD.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png";
if (openFD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Operation cancelled !");
}
else
{
chosen_file = openFD.FileName;
MemberPics.Image = Image.FromFile(chosen_file);
MemberPics.Image.Save("d:\\Pictures\\chosen_file", System.Drawing.Imaging.ImageFormat.Jpeg);
FileStream fs = new FileStream(chosen_file, FileMode.Open, ileAccess.Read);
_picture = new byte[fs.Length];
fs.Read(fileData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
}
}
Now you can insert _picture to the BLOB field.
If you need more help let me know.
JONAS BOATENG24 reputationPosted Oct 21, 2009, 12:11 AM EDT
Sreekanth199 reputationPosted Oct 21, 2009, 12:28 AM EDT
string fileName = Server.MapPath(".")+"\\"+Path.GetFileName(fileUpload.Value);
fileUpload.PostedFile.SaveAs(fileName);
this is the traditional method of uploading here we get
filename (string) as excat path of tat file...and we can store it to databse and display...
JONAS BOATENG24 reputationPosted Oct 21, 2009, 4:16 AM EDT
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Oct 21, 2009, 8:28 PM EDT
It's because you are saving the full path of the image to the filenames table. Instead of saving the full path, save only the file name.
private void btnpics_Click(object sender, EventArgs e)
{
OpenFileDialog openFD = new OpenFileDialog();
//string chosen_file = "";
openFD.Title = "Insert an image ";
openFD.InitialDirectory = "c:";
openFD.FileName = "";
openFD.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png";
if (openFD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Operation cancelled !");
}
else
{
string chosen_file = openFD.FileName;
MemberPics.Image = Image.FromFile(chosen_file);
string filename = Path.GetFileName(chosen_file);
MemberPics.Image.Save(string.Format("d:\\Pictures\\{0}", filename), System.Drawing.Imaging.ImageFormat.Jpeg);
//Save the value of 'filename' variable to your table.
}
}
Then after you retrieve the filename from the DB, you should create the full path like this.
Let's say you retrieve it and assigned it to the variable 'filenamefromdb'.
Then you get the full path of the image.
string fullpath = string.Format("d:\\Pictures\\{0}", filenamefromdb);
Then use this full path to find the image.
MemberPics2.Image = Image.FromFile(fullpath)
If this answer helps you, please accept my answer.
Master BillaRank #2,229 · 630 reputationPosted Oct 22, 2009, 12:20 AM EDT
We ned code like this way
We can do easier way
Private Sub display_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim path As String
Dim pic As Image
OpenFileDialog1.ShowDialog()
pic = New Bitmap(OpenFileDialog1.FileName)
PictureBox1.Image = pic
End Sub
Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
Dim path As String
Dim pic As Image
pic = PictureBox1.Image
SaveFileDialog1.ShowDialog()
pic.Save(SaveFileDialog1.FileName)
End Sub
thank you
JONAS BOATENG24 reputationPosted Oct 22, 2009, 1:37 AM EDT
[code]
string strSQL = "INSERT INTO memberinfo " + "(memberid,memberSname,memberFname, picture, fileName)" + "VALUES( '" + txtmid.Text + "','" + txtmSname.Text + "','" + txtFname.Text + "', '" + MemberPics.Image +"', '" + fileName+"')";can i write a code like below to retrieve the fileName and assign it to the picturebox called display like this?
[code]
string loadPic = "SELECT fileName from memberinfo";
[/code]
If not, then how do i retrieve the fileName from the database and load it into display picturebox.
JONAS BOATENG24 reputationPosted Oct 22, 2009, 10:43 AM EDT
[code]
string loadPic = "SELECT fileName from memberinfo";
string fullpath = string.Format("d:\\Pictures\\{0}", loadPic);
MemberPics.Image = Image.FromFile(fullpath);
[/code]
but am getting a FileNotFound error. What cud be the problem cos the filename is in the table and the picture isn also in the folder d:\\pictures. please help
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Oct 22, 2009, 11:11 PM EDT
I think the problem is in this part.
string loadPic = "SELECT fileName from memberinfo";
According to what you have done, it looks for a file named 'SELECT fileName from memberinfo' at 'd:\\Pictures this folder. That's why you get the exception.
How do you execute this query? Only after executing this query, you will get the file name from the DB. Then only you can assign the value to 'loadpic'.
If you tell me the data base that you use I can send you the code to read the file name from the table. Better if you can send me the code you used to save it to DB.
JONAS BOATENG24 reputationPosted Oct 23, 2009, 12:31 AM EDT
[code]
MySqlConnection mycon = new MySqlConnection("datasource=localhost;username=root;password=112358132132;database=church");
string strSQL = "INSERT INTO memberinfo " + "(memberid,memberSname,memberFname, fileName)" + "VALUES( '" + txtmid.Text + "','" + txtmSname.Text + "','" + txtFname.Text + "','" + file +"')";
accessDB.myCmd(strSQL).ExecuteNonQuery();
[/code]
the above code successfully inserted the fileName into the table. Then i want to use this the retrieve the fileName and add it to the path to get the file from the folder
[code]
string loadPic = "SELECT fileName from memberinfo";
string fullpath = string.Format("d:\\Pictures\\{0}.jpg", loadPic);
MemberPics.Image = Image.FromFile(fullpath);
[/code]
So now I dont know how to query the fileName and add it to the path to access the file
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Oct 23, 2009, 4:37 AM EDT
MySqlConnection mycon = new MySqlConnection("datasource=localhost;username=root;password=112358132132;database=church"); MySqlClient.MySqlDataReader read; string query = "SELECT fileName from memberinfo"; read = accessDB.myCmd(query).ExecuteReader(); string loadPic; while (read.Read()) { loadPic = read.GetString(1); } read.Close(); string fullpath = string.Format("d:\\Pictures\\{0}.jpg", loadPic); MemberPics.Image = Image.FromFile(fullpath);JONAS BOATENG24 reputationPosted Oct 23, 2009, 7:50 AM EDT
public void loaddetails() { MySqlDataReader reader = null; try { accessDB.connection(); //this.reset(); String search = "SELECT memberSname,memberFname,memberOname,membersex,fileName FROM memberInfo WHERE memberid = '" + txtmid.Text + "'"; accessDB.myCmd(search).ExecuteNonQuery(); reader = accessDB.myCmd(search).ExecuteReader(); reader.Read(); //while () //{ txtmSname.Text = reader["memberSname"].ToString(); txtFname.Text = reader["memberFname"].ToString(); txtmOname.Text = reader["memberOname"].ToString(); dateDOB.Text = reader["memberDOB"].ToString(); cmbGender.Text = reader["membersex"].ToString(); string img = reader["fileName"].ToString(); try { String path = ("d:\\Pictures\\" + img + ".jpg"); path = path.Trim(Path.GetInvalidFileNameChars()); path = path.Trim(Path.GetInvalidPathChars()); MemberPics.Image = Image.FromFile(path); } catch (Exception ex) { MessageBox.Show("Sorry, the pics could not be loaded " + ex.ToString()); } reader.Close();JONAS BOATENG24 reputationPosted Oct 23, 2009, 8:24 AM EDT