472,791 Members | 958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Retrieving images from MS SQL databse


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()+"--------------");
  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.             {
  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.             }
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 2364
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();
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);
Dec 15 '07 #2

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

Similar topics

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...
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...
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...
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...
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...
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...
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,...
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...
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 =...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.