473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2408
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

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

Similar topics

6
by: Alec | last post by:
Newbie question. I have a database for displaying the names of bed and breakfasts searched for by the town they are in as below. <?php $result = @mysql_query ("SELECT name FROM...
1
by: Roman Kagan | last post by:
Hi everyone, I am successful in retrieving the image from the table (.TIF), however, the retrieved file does not work. It looks like an image file - extension, size and all, but the image is not...
0
by: Dirk Vervecken | last post by:
Hi, i'm having a bit af trouble with a datarepeater of mine. It shows the data that I want, but i've also implemented an Imagebutton that, when clicked, needs to retrieve the information from the...
7
by: Sirplaya | last post by:
I am retrieving images that I stored in SQL Server on my web pages in C#. I have no problem with the images displaying, however, I am trying to wrap the image with an <A HREF ..." and each time I...
1
by: charlesg | last post by:
I am seriously i need for a solution to this problem fast.I have just inherited an SQL server DB where the images were stored in the database using a table mandatepictures.The images column is...
1
by: lmwasisebe | last post by:
hie guys i having problems retrieving xml values, this is the structure of the problem: I have a database with the following fields, 1)id 2)state 3)request. the request field contains a xml file...
2
by: Bjorn Sagbakken | last post by:
This is kind of silly, but I just cannot figure out why: In ASP.NET 2.0 I am retrieving images from a MS SQL server, and writing them as binary to a separate page as the url for image controls,...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
2
by: shivapadma | last post by:
i have inserted the image into database using the following code String driverName = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/"; String dbName =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.