Save and Retrive Image

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

Join Date: Feb 2009
Posts: 3,215
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: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #11
Oct 22nd, 2009
A lot of people are against storing binary data in an SQL Server. "Dont use it is a file system!". I personally don't share those opinions and often will store images in an SQL database when I can't ensure a UNC path will be accessible to all machines running the application.

So once you answer DdoubleD's question about storing the image inside of the database we should be better able to help you.
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
  #12
Oct 23rd, 2009
sknake: Now all i want to do is to retrieve the filename from the database and add it to my path to a folder. I am not sure I want to go through the long process of trying to retrieve the image from the database. So i need help with executing the query after successfully retrieving the filename from the database and then assign it to my picturebox Memberpics.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
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: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #13
Oct 23rd, 2009
Try something like this. You need to use a "Where" clause in your SQL Query so you don't select every row since you probably only want the image for a particular user. Here is the test table I created:

  1. --IF OBJECT_ID('MemberInfo', 'U') IS NOT NULL DROP TABLE MemberInfo
  2. CREATE TABLE MemberInfo
  3. (
  4. MemberId INT PRIMARY KEY,
  5. MemberSname VARCHAR(50),
  6. MemberFName VARCHAR(50),
  7. Picture VARCHAR(50),
  8. FName VARCHAR(50)
  9. )

And the code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Data.SqlClient;
  11.  
  12. namespace daniweb
  13. {
  14. public partial class frmInsertImage : Form
  15. {
  16. const string connStr = @"Data Source=apex2006sql;Initial Catalog=ServManIRC;Integrated Security=True;";
  17.  
  18. public frmInsertImage()
  19. {
  20. InitializeComponent();
  21. }
  22.  
  23. private void button1_Click(object sender, EventArgs e)
  24. {
  25. using (OpenFileDialog openFD = new OpenFileDialog())
  26. {
  27. openFD.Title = "Insert an image ";
  28. openFD.InitialDirectory = "c:";
  29. openFD.FileName = "";
  30. openFD.Filter = "JPEG Image|*.jpg|GIF Image|*.gif|PNG Image|*.png";
  31. openFD.Multiselect = false;
  32.  
  33. if (openFD.ShowDialog() != DialogResult.OK)
  34. return;
  35.  
  36. const string new_dir = @"C:\pictures\"; //I use C, not D
  37. string fName = System.IO.Path.Combine(new_dir, System.IO.Path.GetFileName(openFD.FileName));
  38. if (File.Exists(fName))
  39. File.Delete(fName);
  40. System.IO.File.Copy(openFD.FileName, fName);
  41. string msg = string.Format("Copied {0} to {1}",
  42. openFD.FileName,
  43. fName);
  44. MessageBox.Show(msg, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  45.  
  46. //Insert into database
  47. int memberId = 1;
  48. string membersname = "knake";
  49. string memberfname = "scott";
  50. string picture = "my profile picture";
  51.  
  52. InsertUpdateMemberInfo(memberId, membersname, memberfname, picture, fName);
  53. }
  54. }
  55.  
  56. private void button2_Click(object sender, EventArgs e)
  57. {
  58. pictureBox1.Image = GetMemberImage(1);
  59. }
  60.  
  61.  
  62.  
  63. private static void InsertUpdateMemberInfo(int MemberId, string MemberSname, string MemberFName, string Picture, string FName)
  64. {
  65. List<string> Sql = new List<string>();
  66. Sql.Add("IF EXISTS (Select * From MemberInfo (NOLOCK) Where MemberId = @MemberId)");
  67. Sql.Add("BEGIN");
  68. Sql.Add(" Update MemberInfo");
  69. Sql.Add(" Set ");
  70. Sql.Add(" MemberSname = @MemberSname,");
  71. Sql.Add(" MemberFName = @MemberFName,");
  72. Sql.Add(" Picture = @Picture,");
  73. Sql.Add(" FName = @FName");
  74. Sql.Add(" Where MemberId = @MemberId");
  75. Sql.Add(" Select @MemberId");
  76. Sql.Add("END ELSE");
  77. Sql.Add("BEGIN");
  78. Sql.Add(" Insert Into MemberInfo (MemberId, MemberSname, MemberFName, Picture, FName) Values (@MemberId, @MemberSname, @MemberFName, @Picture, @FName)");
  79. Sql.Add(" Select Cast(SCOPE_IDENTITY() as int)");
  80. Sql.Add("END");
  81. string query = GetText(Sql);
  82. using (SqlConnection conn = new SqlConnection(connStr))
  83. {
  84. conn.Open();
  85. using (SqlCommand cmd = new SqlCommand(query, conn))
  86. {
  87. cmd.Parameters.Add(new SqlParameter("@MemberId", SqlDbType.Int)).Value = MemberId;
  88. cmd.Parameters.Add(new SqlParameter("@MemberSname", SqlDbType.VarChar)).Value = MemberSname;
  89. cmd.Parameters.Add(new SqlParameter("@MemberFName", SqlDbType.VarChar)).Value = MemberFName;
  90. cmd.Parameters.Add(new SqlParameter("@Picture", SqlDbType.VarChar)).Value = Picture;
  91. cmd.Parameters.Add(new SqlParameter("@FName", SqlDbType.VarChar)).Value = FName;
  92. cmd.ExecuteNonQuery();
  93. }
  94. }
  95. }
  96.  
  97. private static string GetText(List<string> Sql)
  98. {
  99. StringBuilder sb = new StringBuilder();
  100. foreach (string s in Sql)
  101. sb.AppendLine(s);
  102. return sb.ToString().Trim();
  103. }
  104.  
  105. private static string GetMemberFilePath(int memberid)
  106. {
  107. const string query = @"Select FName From MemberInfo Where MemberId = @MemberId";
  108. using (SqlConnection conn = new SqlConnection(connStr))
  109. {
  110. conn.Open();
  111. using (SqlCommand cmd = new SqlCommand(query, conn))
  112. {
  113. cmd.Parameters.Add(new SqlParameter("@MemberId", SqlDbType.Int)).Value = memberid;
  114. string result = Convert.ToString(cmd.ExecuteScalar());
  115. return result;
  116. }
  117. }
  118. }
  119. private static Image GetMemberImage(int memberid)
  120. {
  121. string fileName = GetMemberFilePath(memberid);
  122.  
  123. try
  124. {
  125. if (string.IsNullOrEmpty(fileName) || !File.Exists(fileName))
  126. {
  127. return null;
  128. }
  129. }
  130. catch (IOException)
  131. {
  132. return null;
  133. }
  134.  
  135. return Image.FromFile(fileName);
  136. }
  137. }
  138. }
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
  #14
Oct 23rd, 2009
Below is the code to read from the database into the controls i have on the form. everything is reading except the image. For that i get "Illegal characters in path" exception. can anyone help with the reader for the image?
  1. public void loaddetails()
  2. {
  3.  
  4. MySqlDataReader reader = null;
  5.  
  6. try
  7. {
  8. accessDB.connection();
  9. //this.reset();
  10.  
  11. String search = "SELECT memberSname,memberFname,memberOname,membersex,fileName FROM memberInfo WHERE memberid = '" + txtmid.Text + "'";
  12.  
  13. accessDB.myCmd(search).ExecuteNonQuery();
  14.  
  15. reader = accessDB.myCmd(search).ExecuteReader();
  16.  
  17. reader.Read();
  18. //while ()
  19. //{
  20. txtmSname.Text = reader["memberSname"].ToString();
  21. txtFname.Text = reader["memberFname"].ToString();
  22. txtmOname.Text = reader["memberOname"].ToString();
  23. dateDOB.Text = reader["memberDOB"].ToString();
  24. cmbGender.Text = reader["membersex"].ToString();
  25. string img = reader["fileName"].ToString();
  26.  
  27. try
  28. {
  29. String path = ("d:\\Pictures\\" + img + ".jpg");
  30. path = path.Trim(Path.GetInvalidFileNameChars());
  31. path = path.Trim(Path.GetInvalidPathChars());
  32. MemberPics.Image = Image.FromFile(path);
  33. }
  34. catch (Exception ex)
  35. {
  36.  
  37. MessageBox.Show("Sorry, the pics could not be loaded " + ex.ToString());
  38. }
  39.  
  40. reader.Close();

I just want to read the filename and assign it to the path
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,215
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: 573
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #15
Oct 23rd, 2009
Then you have illegal characters in the path of your filename, ie you probably inserted the wrong data in the table.

"C:\@$1298035@!#@%%%__@(\filename.txt" <-- illegal characters in path
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
  #16
Oct 23rd, 2009
But I can see the name of the image I selected in the table, so what cud be the problem and how can I solve it. any code or example will do Please
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 908
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
  #17
Oct 23rd, 2009
Originally Posted by SAINTJAB View Post
But I can see the name of the image I selected in the table, so what cud be the problem and how can I solve it. any code or example will do Please
Post your current code, indicating the line you get the error, and also indicate the values being used in the call.
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
  #18
Oct 23rd, 2009
  1. 1.
  2. public void loaddetails()
  3. 2.
  4. {
  5. 3.
  6.  
  7. 4.
  8. MySqlDataReader reader = null;
  9. 5.
  10.  
  11. 6.
  12. try
  13. 7.
  14. {
  15. 8.
  16. accessDB.connection();
  17. 9.
  18. //this.reset();
  19. 10.
  20.  
  21. 11.
  22. String search = "SELECT memberSname,memberFname,memberOname,membersex,fileName FROM memberInfo WHERE memberid = '" + txtmid.Text + "'";
  23. 12.
  24.  
  25. 13.
  26. accessDB.myCmd(search).ExecuteNonQuery();
  27. 14.
  28.  
  29. 15.
  30. reader = accessDB.myCmd(search).ExecuteReader();
  31. 16.
  32.  
  33. 17.
  34. reader.Read();
  35. 18.
  36. //while ()
  37. 19.
  38. //{
  39. 20.
  40. txtmSname.Text = reader["memberSname"].ToString();
  41. 21.
  42. txtFname.Text = reader["memberFname"].ToString();
  43. 22.
  44. txtmOname.Text = reader["memberOname"].ToString();
  45. 23.
  46. dateDOB.Text = reader["memberDOB"].ToString();
  47. 24.
  48. cmbGender.Text = reader["membersex"].ToString();
  49. 25.
  50. string img = reader["fileName"].ToString();
  51. 26.
  52.  
  53. 27.
  54. try
  55. 28.
  56. {
  57. 29.
  58. String path = ("d:\\Pictures\\" + img + ".jpg");
  59. 30.
  60. path = path.Trim(Path.GetInvalidFileNameChars());
  61. 31.
  62. path = path.Trim(Path.GetInvalidPathChars());
  63. 32.
  64. MemberPics.Image = Image.FromFile(path);
  65. 33.
  66. }
  67. 34.
  68. catch (Exception ex)
  69. 35.
  70. {
  71. 36.
  72.  
  73. 37.
  74. MessageBox.Show("Sorry, the pics could not be loaded " + ex.ToString());
  75. 38.
  76. }
  77. 39.
  78.  
  79. 40.
  80. reader.Close();

Above is the code and the error is in line 32: Illegal character in path
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 908
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
  #19
Oct 23rd, 2009
What is the value in path when it reaches line 32?
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
  #20
Oct 23rd, 2009
After running the code, I see path = "d:\\Pictures\\DA\0\0\0\0\0\0\0\0\0\0\0.jpg" instend of d:\\Pictures\\DA.jpg. Can someone help me to trim the unwanted characters from the path cos the fileName is DA so the path should read d:\\Pictures\DA.jpg. And I see only DA in the table not DA\0\0\0\0\0\0\0\0\0\0\0.
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