| | |
Save and Retrive Image
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2009
Posts: 14
Reputation:
Solved Threads: 0
Hi Guys, i have a code to display an image in a picturebox called MemberPics. this image is also saved into a folder D:\\pictures. My problem is eventhough the image is saved successfully, am not able to retrieve it in another form. I always have FileNotFoundException even when I can physically see the file in the folder. please can anyone help me out. this is the code to display and save the image
Later when i use this, the FileNotFoundException is thrown at me after successfuly saving a user info and i want to load it into another picturebox
C# Syntax (Toggle Plain Text)
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.jpg",System.Drawing.Imaging.ImageFormat.Jpeg); MemberPics.Image.Save("d:\\Pictures\\chosen_file", System.Drawing.Imaging.ImageFormat.Jpeg);*/ using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.ShowDialog(); //string fileName = System.IO.Path.GetFileName(ofd.FileName); fileName = System.IO.Path.GetFileName(ofd.FileName); System.IO.File.Copy(ofd.FileName, "D:\\Pictures\\" + fileName); MemberPics.Image = Image.FromFile("D:\\Pictures\\" + fileName); }
Later when i use this, the FileNotFoundException is thrown at me after successfuly saving a user info and i want to load it into another picturebox
C# Syntax (Toggle Plain Text)
display.Image = Image.FromFile("D:\\Pictures\\" + fileName);
•
•
Join Date: Sep 2009
Posts: 45
Reputation:
Solved Threads: 9
0
#2 Oct 21st, 2009
Are you trying same file,i am not sure is this is the issue,but test with different files.
Chandru
SilverlightScripting.com
SilverlightScripting.com
•
•
Join Date: Jul 2009
Posts: 905
Reputation:
Solved Threads: 145
0
#3 Oct 21st, 2009
If you have positively verified that the file you can see is named exactly the same as the file you are attempting to load...
Is it possible the file is still locked by a prior call to Image.FromFile? The MSDN documentation does not say what kind of lock is placed on the file (only that it remains locked until the image is disposed), but if there is a write lock then that would explain an exception, but not necessarily the one you are getting...
Is it possible the file is still locked by a prior call to Image.FromFile? The MSDN documentation does not say what kind of lock is placed on the file (only that it remains locked until the image is disposed), but if there is a write lock then that would explain an exception, but not necessarily the one you are getting...
•
•
Join Date: Sep 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#4 Oct 21st, 2009
•
•
•
•
If you have positively verified that the file you can see is named exactly the same as the file you are attempting to load...
Is it possible the file is still locked by a prior call to Image.FromFile? The MSDN documentation does not say what kind of lock is placed on the file (only that it remains locked until the image is disposed), but if there is a write lock then that would explain an exception, but not necessarily the one you are getting...
•
•
Join Date: Jul 2009
Posts: 905
Reputation:
Solved Threads: 145
0
#5 Oct 21st, 2009
•
•
•
•
how do I unlock the file before displaying it on another picturebox
C# Syntax (Toggle Plain Text)
public Image ImageFromFileNoLock(string fileName) { // Image.FromFile will hold a lock on the file until the image is destroyed, // so using a FileStream to load the file will release lock before returning image // because the stream gets closed... FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); // Get image from stream... Image img = Image.FromStream(fs); // Close/release the file... fs.Close(); // return the image... return img; }
0
#6 Oct 22nd, 2009
I don't understand what your code is doing. You are making 2 calls to
Try something like this:
openFileDiag.ShowDialog() .Try something like this:
C# Syntax (Toggle Plain Text)
private void button2_Click(object sender, EventArgs e) { using (OpenFileDialog openFD = new OpenFileDialog()) { openFD.Title = "Insert an image "; openFD.InitialDirectory = "c:"; openFD.FileName = ""; openFD.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png"; openFD.Multiselect = false; if (openFD.ShowDialog() != DialogResult.OK) return; const string new_dir = @"C:\pictures\"; //I use C, not D string fName = System.IO.Path.Combine(new_dir, System.IO.Path.GetFileName(openFD.FileName)); System.IO.File.Copy(openFD.FileName, fName); string msg = string.Format("Copied {0} to {1}", openFD.FileName, fName); MessageBox.Show(msg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } }
•
•
Join Date: Sep 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#7 Oct 22nd, 2009
@sknake: Thanks, I just changed it. But now, I want to store the fileName and the path into MySql database so that the can return the mysql string and load it into another picturebox bcos am now able to store the image from the picturebo (MemberPics) into mysql. Like this
and called the picture and path to another picture box like this
Is that possible?, bcos I want to load the picture from mysql table which contains some other info about a particular user so that i can get the particular picture from that member info
C# Syntax (Toggle Plain Text)
string strSQL = "INSERT INTO memberinfo " + "(memberid,memberSname,memberFname, picture, fileName)" + "VALUES( '" + txtmid.Text + "','" + txtmSname.Text + "','" + txtFname.Text + "', '" + MemberPics.Image +"', '" + fileName+"')";
and called the picture and path to another picture box like this
C# Syntax (Toggle Plain Text)
string loadPic = "SELECT fileName from memberinfo"; display.Image = Image.FromFile("d:\\Pictures\\" + fileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg, loadPic);
Is that possible?, bcos I want to load the picture from mysql table which contains some other info about a particular user so that i can get the particular picture from that member info
•
•
Join Date: Sep 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#9 Oct 22nd, 2009
Guys, i have been able to successfully save the filename called fileName to the table called memberinfo. i can see the filename in the table and also the picture with that name in the folder. Now I want to retrieve the filename from the table and add it to the folder so that my picturebox MemberPics can point to it and load an image from that file using this codes
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
C# Syntax (Toggle Plain Text)
string loadPic = "SELECT fileName from memberinfo"; string fullpath = string.Format("d:\\Pictures\\{0}", loadPic); MemberPics.Image = Image.FromFile(fullpath);
•
•
Join Date: Jul 2009
Posts: 905
Reputation:
Solved Threads: 145
0
#10 Oct 22nd, 2009
In line 2, all you are doing is appending your select statement to fullpath., which means you are trying to open file: "d:\\Pictures\\SELECT fileName from memberinfo", which does not exist I would imagine.
You need to retrieve "filename" from your database first, if that is where it is stored. You have the select statement (it appears) and I believe you said you saved the filename in your DB, right?
EDIT: Oh, I have seen you are also wanting to store and retrieve the actual image file from your database, which you have not yet achieved either?
You need to retrieve "filename" from your database first, if that is where it is stored. You have the select statement (it appears) and I believe you said you saved the filename in your DB, right?
EDIT: Oh, I have seen you are also wanting to store and retrieve the actual image file from your database, which you have not yet achieved either?
Last edited by DdoubleD; Oct 22nd, 2009 at 4:27 pm.
![]() |
Similar Threads
- how to retrive image file from mysql databse using php (PHP)
- How to store a byte array in a database and retrive it back as byte array? (C#)
- How to Save data and image in a MSAccess Database using vb.net (VB.NET)
- save GDI+ image into sql 2005 database (C#)
Other Threads in the C# Forum
- Previous Thread: how to filter strange characters?
- Next Thread: SQL querry Combo Box Question
| Thread Tools | Search this Thread |
animation api applet array back backup bmp broken button byte clone cloning code directory disk displayimageinsteadofflash drive file flash gdi ghost google hard header hosting image images java jpanel jpeg link mediawiki method multimedia mysql open panel php picture picturebox problem random reading reputationmanagement screen scroll search shot subdomain swf swf. swing transfer upload url web website windows







