By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,314 Members | 2,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,314 IT Pros & Developers. It's quick & easy.

problem with saving an image in database.

P: 5
hai to all...!!
iam working with Ms Access 2000.
iam saving an image in the database.
After that i want to retrive the same image from database and displaying it in the picture box.
but its not working. why it is happening?
please help me.
iam sending the code.
when button1 clicks it store in database.
when button2 clicks it should display in picturebox.

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.             if( DialogResult.OK == FileOpenDlg.ShowDialog() )
  5.         {
  6.         System.IO.FileStream stream = new System.IO.FileStream(FileOpenDlg.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  7.         byte[] buffer = new byte[stream.Length];
  8.         stream.Read(buffer, 0, (int)stream.Length);
  9.         stream.Close();
  10.                 string strName = System.IO.Path.GetFileNameWithoutExtension(FileOpenDlg.FileName);
  11.                 string connstring = "provider=microsoft.jet.oledb.4.0;Data Source=D:\\srinu\\database\\db1.mdb";
  12.                 OleDbConnection conn = new OleDbConnection(connstring);
  13.                 conn.Open();
  14.                 string sql = "insert into FileInfo values(1,'name','" + buffer + "')";
  15.              OleDbCommand cmd = new OleDbCommand(sql, conn);
  16.                 cmd.ExecuteNonQuery();
  17.                 conn.Close();
  18.          }
  19.  
  20.  
  21.   private void button2_Click(object sender, EventArgs e)
  22.         {
  23.             try
  24.             {
  25.                 string connstring = "provider=microsoft.jet.oledb.4.0;data source=D:\\srinu\\database\\db1.mdb";
  26.                 OleDbConnection conn = new OleDbConnection(connstring);
  27.                 conn.Open();
  28.                 string query = "Select FullName From FileInfo";
  29.                 OleDbCommand cmd = new OleDbCommand(query, conn);
  30.                    byte[] b = (byte[])cmd.ExecuteScalar();
  31.                 MessageBox.Show(b.Length.ToString());
  32.                 if(b.Length > 0)
  33.                 {
  34.                 System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);
  35.                 stream.Write(b, 0, b.Length);
  36.                 DrawToScale(new Bitmap(stream));
  37.                 stream.Close();
  38.              }
  39.  
  40.             }
  41.             catch (Exception ex)
  42.             {
  43.                 MessageBox.Show(ex.ToString());
  44.             }
  45.         }
  46.  
  47.         private void DrawToScale(Image bmp)
  48.         {          
  49.             Rectangle rc = pictureBox1.ClientRectangle;
  50.             SizeF size = new SizeF( bmp.Width / bmp.HorizontalResolution, bmp.Height / bmp.VerticalResolution);
  51.             float fScale = Math.Min( rc.Width / size.Width, rc.Height / size.Height);
  52.             size.Width *= fScale;
  53.             size.Height *= fScale;
  54.             pictureBox1.Image = new Bitmap(bmp, size.ToSize());
  55.             pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
  56.  
  57.         }


what mistake is iam doing?
i think it is correct method. but its not working.
please help me.
thank u very much in advance.....
Feb 12 '08 #1
Share this Question
Share on Google+
2 Replies


debasisdas
Expert 5K+
P: 8,127
Are you getting any error ?

Run the code the code in Debug mode and check.
Feb 12 '08 #2

P: 5
Are you getting any error ?

Run the code the code in Debug mode and check.
hai.....!!
thank u very much for ur reply..
i already run this code in debug mode. I thought that the problem is while saving the image in database. When i convert the image into bite array, it size is 1550044. When i retriving it from database then the bite array size is 26 only.
So i think the image is not saved correctly.
why its happenning?
iam getting an exception like this.
system.ArgumentException:perameter is not valid.
at system.Drawing.Bitmap..ctor(Stream stream)

please help me.
thank u.
Feb 13 '08 #3

Post your reply

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