Save and Retrive Image

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 972
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: 213
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #21
Oct 23rd, 2009
We should probably determine why you got those null characters ( \0 stored for your "filename" in the first place. What is the code you are using to INSERT or UPDATE the "filename" field?
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
  #22
Oct 23rd, 2009
I used this code to save the image to the folder
  1. string chosen_file = "";
  2. string file = "";
  3. chosen_file = openFD.FileName;
  4. string fileName = System.IO.Path.GetFileNameWithoutExtension(chosen_file);
  5. MemberPics.Image = Image.FromFile(chosen_file);
  6. MemberPics.Image.Save(string.Format("d:\\Pictures\\{0}.jpg", fileName), System.Drawing.Imaging.ImageFormat.Jpeg);
  7. file = fileName;

Then this code to insert the filename (fileName into the mysql database

  1. string strSQL = "INSERT INTO memberinfo " + "(memberid,memberSname,memberFname, fileName)" + "VALUES( '" + txtmid.Text + "','" + txtmSname.Text + "','" + txtFname.Text + "', '" + file +"')";
  2.  
  3. accessDB.myCmd(strSQL).ExecuteNonQuery();
which was successful. So after I was able to save the file into the folder and the fileName(not including the weird character) into the db, I expected to find only the filename not other characters attached to the fileName
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 972
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: 213
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #23
Oct 23rd, 2009
I am going to BUMP on this one because nothing I can see to cause that string assignment to occur that way.

I assume you added the following lines to try to elliminate the problem:

  1. path = path.Trim(Path.GetInvalidFileNameChars());
  2. path = path.Trim(Path.GetInvalidPathChars());

but, you shouldn't need to be doing that either given the code I've seen. Also, I have not used those calls in a string.Trim before--have you tried it without those two lines?

Anyway, I've got somewhere I need to get to... Just post back a response and someone else will surely look into this before I get back.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 63
Reputation: mikiurban is an unknown quantity at this point 
Solved Threads: 17
mikiurban mikiurban is offline Offline
Junior Poster in Training
 
0
  #24
Oct 23rd, 2009
The column in your database that holds the filename might be a char data type column (or wchar, etc), if you change it to varchar (or nvarchar, etc) then it will let you use variable length strings and not give you all those extra zeros.

The other choice (off the top of my head) is to change line 29 to this:
  1. String path = ("d:\\Pictures\\" + img.Trim() + ".jpg");

Edit: (for some reason I think the char data type pads with spaces, not zeros, so the column data type couple be wrong. Also, I forget if Trim() removes zeros like this, you might need to call
  1. img.Trim(new char[] {'\0'})
instead which specifically removes the zeros ... I think
Last edited by mikiurban; Oct 23rd, 2009 at 4:53 pm. Reason: Disclaimer: I just ate a giant chicken parmesan, I blame all errors to impending food coma :)
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
  #25
Oct 23rd, 2009
path = path.Replace("\0", "");
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,444
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: 627
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #26
Oct 23rd, 2009
What in the world are you doing? Try to use the code I provided you and get it working. Then modify the code to do what you want it to .. and find out where it breaks.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 63
Reputation: mikiurban is an unknown quantity at this point 
Solved Threads: 17
mikiurban mikiurban is offline Offline
Junior Poster in Training
 
1
  #27
Oct 23rd, 2009
sknake has a point (and no, he doesn't pay me to agree with him), calling .Replace() will fix this problem for now, but what you really want to do is understand why it's happening in the first place, or else your program will have a million Replace() commands. My money is on the data stored as char instead of varchar in the DB.

I doubt this is the problem, but you might want to be careful with this line:
  1. string strSQL = "INSERT INTO memberinfo " + "(memberid,memberSname,memberFname, fileName)" + "VALUES( '" + txtmid.Text + "','" + txtmSname.Text + "','" + txtFname.Text + "', '" + file +"')";

If any of those text boxes have an apostrophe in there, the insert will break. I suggest using
  1. OleDbCommand cmd = new OleDbCommand(...);
  2. cmd.Parameters.AddWithValue(...)
(or OdbcCommand, I forget which is for MS Access) to prevent any kind of query hacking shenanigans.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,444
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: 627
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #28
Oct 24th, 2009
Originally Posted by mikiurban View Post
sknake has a point (and no, he doesn't pay me to agree with him), calling .Replace() will fix this problem for now, but what you really want to do is understand why it's happening in the first place, or else your program will have a million Replace() commands. My money is on the data stored as char instead of varchar in the DB.
mikiurban has hit the nail on the head. On another note you need to handle DBNull.Value . I call Convert.ToString() on all string values brought back and the NULL will equate to string.Empty .
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Tags
image

Message:




Views: 2004 | Replies: 27
Thread Tools Search this Thread



Tag cloud for image
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC