469,948 Members | 2,913 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,948 developers. It's quick & easy.

Retrieving images from MS SQL databse

Hi,

Im using a MS SQL server to store images... Im creating a windows form application using C#.

I need to store and retrieve images from the database.

This is my code to store images:

Expand|Select|Wrap|Line Numbers
  1.              pictureBox.Image = new  System.Drawing.Bitmap(this.openFileDialog.FileName);
  2.              MemoryStream mstr = new MemoryStream();
  3.              pictureBox.Image.Save(mstr, pictureBox.Image.RawFormat);
  4.              byte[] arrImage = mstr.GetBuffer();
  5.              Console.WriteLine("----------"+arrImage.ToString()+"--------------");
  6.  
  7.             string connString = SatSmart.Misc.DataConn.connString;
  8.             string qry = "update " + this.screenObject.loggedInUserType + "s set photo='@Pic' where userName='" + this.screenObject.loggedInUserName + "';";
  9.             System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection(connString);
  10.             System.Data.Odbc.OdbcCommand cmd = new System.Data.Odbc.OdbcCommand(qry, conn);
  11.             System.Data.Odbc.OdbcParameter param=new System.Data.Odbc.OdbcParameter("@Pic",System.Data.Odbc.OdbcType.Image);
  12.             param.Value = arrImage;
  13.             cmd.Parameters.Add(param);
  14.             conn.Open();
  15.             cmd.ExecuteNonQuery();
  16.             conn.Close();
This loading image code works fine...

Here is my code for retrieving images:

Expand|Select|Wrap|Line Numbers
  1. while (reader.Read())
  2.             {
  3.  
  4.                 byte[] image = (byte[])reader.GetValue(reader.GetOrdinal("photo"));
  5.                 System.Drawing.Image newImage = null;
  6.                 using (System.IO.MemoryStream stream = new System.IO.MemoryStream(image))
  7.                 {
  8.                     try
  9.                     {
  10.                         newImage = System.Drawing.Image.FromStream(stream);
  11.                     }
  12.                     catch (Exception e)
  13.                     {
  14.                         System.Windows.Forms.MessageBox.Show(e.ToString());
  15.                     }
  16.                 }
  17.                 this.pictureBox.Image = newImage;
  18.             }
  19.  
this code doesnt work.Im getiing a exception...
The Exception stack trace is....
A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.Image.FromStream(Stream stream)
at SatSmart.TeacherGUI.MyAccountUserControl..ctor(scr een screen) in C:\Documents and Settings\san.bak\Desktop\SatSmart\SatSmart\Teacher GUI\MyAccountUserControl.cs:line 94

Its a "System.ArgumentException:Parameter is not valid"

Plz help... Im really stuck in this.....
Dec 15 '07 #1
1 2249
I got it finally...

Here are the code snippets....

To upload image:

Expand|Select|Wrap|Line Numbers
  1. MemoryStream mstr = new MemoryStream();
  2.                 pictureBox.Image.Save(mstr, pictureBox.Image.RawFormat);
  3.                 byte[] picbyte = mstr.GetBuffer();
  4.                 conn.Open ();
  5.                 string qry = "INSERT INTO images(imageData) values(?)";
  6.                 System.Data.Odbc.OdbcCommand cmd= new System.Data.Odbc.OdbcCommand(qry, conn);
  7.                 cmd.Parameters.AddWithValue("@Picture", System.Data.Odbc.OdbcType.Image);
  8.                 cmd.Parameters["@Picture"].Value = picbyte;
  9.                 cmd.ExecuteNonQuery();
  10.  
To download image:

Expand|Select|Wrap|Line Numbers
  1. string qry="select imageData from images where imageId=?";
  2.                 System.Data.Odbc.OdbcCommand cmd= new System.Data.Odbc.OdbcCommand(qry, conn);
  3.                 cmd.Parameters.Add("@ID", System.Data.Odbc.OdbcType.Int, 4);
  4.                 cmd.Parameters["@ID"].Value = reader.GetInt32(reader.GetOrdinal("imageId"));
  5.                 conn.Open();
  6.                 byte[] barrImg=(byte[])cmd.ExecuteScalar();
  7.                 string strfn=Convert.ToString(DateTime.Now.ToFileTime());
  8.                 FileStream fs=new FileStream(strfn, 
  9.                                   FileMode.CreateNew, FileAccess.Write);
  10.                 fs.Write(barrImg,0,barrImg.Length);
  11.                 fs.Flush();
  12.                 fs.Close();
  13.                 this.pictureBox.Image = Image.FromFile(strfn);
  14.  
Dec 15 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Dirk Vervecken | last post: by
7 posts views Thread by Sirplaya | last post: by
1 post views Thread by lmwasisebe | last post: by
2 posts views Thread by Bjorn Sagbakken | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.