Save and Retrive Image

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 14
Reputation: SAINTJAB is an unknown quantity at this point 
Solved Threads: 0
SAINTJAB SAINTJAB is offline Offline
Newbie Poster

Save and Retrive Image

 
0
  #1
Oct 21st, 2009
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

  1. private void btnpics_Click(object sender, EventArgs e)
  2. {
  3.  
  4. OpenFileDialog openFD = new OpenFileDialog();
  5. //string chosen_file = "";
  6.  
  7. openFD.Title = "Insert an image ";
  8. openFD.InitialDirectory = "c:";
  9. openFD.FileName = "";
  10. openFD.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png";
  11.  
  12.  
  13. if (openFD.ShowDialog() == DialogResult.Cancel)
  14. {
  15. MessageBox.Show("Operation cancelled !");
  16. }
  17.  
  18. else
  19. {
  20. /*chosen_file = openFD.FileName;
  21.   MemberPics.Image = Image.FromFile(chosen_file);
  22.   //MemberPics.Image.Save("d:\\Pictures\\Chosen.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
  23.   MemberPics.Image.Save("d:\\Pictures\\chosen_file", System.Drawing.Imaging.ImageFormat.Jpeg);*/
  24. using (OpenFileDialog ofd = new OpenFileDialog())
  25. {
  26. ofd.ShowDialog();
  27. //string fileName = System.IO.Path.GetFileName(ofd.FileName);
  28. fileName = System.IO.Path.GetFileName(ofd.FileName);
  29. System.IO.File.Copy(ofd.FileName, "D:\\Pictures\\" + fileName);
  30. MemberPics.Image = Image.FromFile("D:\\Pictures\\" + fileName);
  31. }

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

  1. display.Image = Image.FromFile("D:\\Pictures\\" + fileName);
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 45
Reputation: chandru7 is an unknown quantity at this point 
Solved Threads: 9
chandru7 chandru7 is offline Offline
Light Poster
 
0
  #2
Oct 21st, 2009
Are you trying same file,i am not sure is this is the issue,but test with different files.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 905
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 145
DdoubleD DdoubleD is offline Offline
Posting Shark
 
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...
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 14
Reputation: SAINTJAB is an unknown quantity at this point 
Solved Threads: 0
SAINTJAB SAINTJAB is offline Offline
Newbie Poster
 
0
  #4
Oct 21st, 2009
Originally Posted by DdoubleD View Post
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...
how do I unlock the file before displaying it on another picturebox
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 905
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 145
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #5
Oct 21st, 2009
Originally Posted by SAINTJAB View Post
how do I unlock the file before displaying it on another picturebox
You can destroy (Dispose() of) the previous Image that was loaded using the FromFile, or you can use this instead of FromFile to return an Image from a file:

  1. public Image ImageFromFileNoLock(string fileName)
  2. {
  3. // Image.FromFile will hold a lock on the file until the image is destroyed,
  4. // so using a FileStream to load the file will release lock before returning image
  5. // because the stream gets closed...
  6. FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  7.  
  8. // Get image from stream...
  9. Image img = Image.FromStream(fs);
  10.  
  11. // Close/release the file...
  12. fs.Close();
  13.  
  14. // return the image...
  15. return img;
  16. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,211
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 572
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast
 
0
  #6
Oct 22nd, 2009
I don't understand what your code is doing. You are making 2 calls to openFileDiag.ShowDialog() .

Try something like this:
  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. using (OpenFileDialog openFD = new OpenFileDialog())
  4. {
  5. openFD.Title = "Insert an image ";
  6. openFD.InitialDirectory = "c:";
  7. openFD.FileName = "";
  8. openFD.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png";
  9. openFD.Multiselect = false;
  10.  
  11. if (openFD.ShowDialog() != DialogResult.OK)
  12. return;
  13.  
  14. const string new_dir = @"C:\pictures\"; //I use C, not D
  15. string fName = System.IO.Path.Combine(new_dir, System.IO.Path.GetFileName(openFD.FileName));
  16. System.IO.File.Copy(openFD.FileName, fName);
  17. string msg = string.Format("Copied {0} to {1}",
  18. openFD.FileName,
  19. fName);
  20. MessageBox.Show(msg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  21. }
  22. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 14
Reputation: SAINTJAB is an unknown quantity at this point 
Solved Threads: 0
SAINTJAB SAINTJAB is offline Offline
Newbie Poster
 
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

  1. 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
  1. string loadPic = "SELECT fileName from memberinfo";
  2. 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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,211
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 572
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast
 
0
  #8
Oct 22nd, 2009
Sure. Just make sure you have the file paths set properly.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 14
Reputation: SAINTJAB is an unknown quantity at this point 
Solved Threads: 0
SAINTJAB SAINTJAB is offline Offline
Newbie Poster
 
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
  1. string loadPic = "SELECT fileName from memberinfo";
  2. string fullpath = string.Format("d:\\Pictures\\{0}", loadPic);
  3. MemberPics.Image = Image.FromFile(fullpath);
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 905
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 145
DdoubleD DdoubleD is offline Offline
Posting Shark
 
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?
Last edited by DdoubleD; Oct 22nd, 2009 at 4:27 pm.
Reply With Quote Quick reply to this message  
Reply

Tags
image

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC