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.
JONAS BOATENGPosted Oct 23, 2009, 12:24 PM
JONAS BOATENGPosted Oct 23, 2009, 11:50 AM
Nilanka DharmadasaPosted Oct 23, 2009, 8:37 AM
JONAS BOATENGPosted Oct 23, 2009, 4:31 AM
[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 DharmadasaPosted Oct 23, 2009, 3:11 AM
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 BOATENGPosted Oct 22, 2009, 2:43 PM
[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
JONAS BOATENGPosted Oct 22, 2009, 5:37 AM
[code]
[/code]
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.
Master BillaPosted Oct 22, 2009, 4:20 AM
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
Nilanka DharmadasaPosted Oct 22, 2009, 12:28 AM
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.
JONAS BOATENGPosted Oct 21, 2009, 8:16 AM
SreekanthPosted Oct 21, 2009, 4:28 AM
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 BOATENGPosted Oct 21, 2009, 4:11 AM
Nilanka DharmadasaPosted Oct 21, 2009, 1:12 AM
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 BOATENGPosted Oct 20, 2009, 2:00 PM
Dushan StankovicPosted Oct 20, 2009, 1:18 PM
Kirtan PatelPosted Oct 20, 2009, 12:47 PM
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);
}