problem with saving an image in database.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • keerthisreenu
    New Member
    • Jan 2008
    • 5

    problem with saving an image in database.

    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.

    [code=vbnet]
    private void button1_Click(o bject sender, EventArgs e)
    {

    if( DialogResult.OK == FileOpenDlg.Sho wDialog() )
    {
    System.IO.FileS tream stream = new System.IO.FileS tream(FileOpenD lg.FileName, System.IO.FileM ode.Open, System.IO.FileA ccess.Read);
    byte[] buffer = new byte[stream.Length];
    stream.Read(buf fer, 0, (int)stream.Len gth);
    stream.Close();
    string strName = System.IO.Path. GetFileNameWith outExtension(Fi leOpenDlg.FileN ame);
    string connstring = "provider=micro soft.jet.oledb. 4.0;Data Source=D:\\srin u\\database\\db 1.mdb";
    OleDbConnection conn = new OleDbConnection (connstring);
    conn.Open();
    string sql = "insert into FileInfo values(1,'name' ,'" + buffer + "')";
    OleDbCommand cmd = new OleDbCommand(sq l, conn);
    cmd.ExecuteNonQ uery();
    conn.Close();
    }


    private void button2_Click(o bject sender, EventArgs e)
    {
    try
    {
    string connstring = "provider=micro soft.jet.oledb. 4.0;data source=D:\\srin u\\database\\db 1.mdb";
    OleDbConnection conn = new OleDbConnection (connstring);
    conn.Open();
    string query = "Select FullName From FileInfo";
    OleDbCommand cmd = new OleDbCommand(qu ery, conn);
    byte[] b = (byte[])cmd.ExecuteSca lar();
    MessageBox.Show (b.Length.ToStr ing());
    if(b.Length > 0)
    {
    System.IO.Memor yStream stream = new System.IO.Memor yStream(b, true);
    stream.Write(b, 0, b.Length);
    DrawToScale(new Bitmap(stream)) ;
    stream.Close();
    }

    }
    catch (Exception ex)
    {
    MessageBox.Show (ex.ToString()) ;
    }
    }

    private void DrawToScale(Ima ge bmp)
    {
    Rectangle rc = pictureBox1.Cli entRectangle;
    SizeF size = new SizeF( bmp.Width / bmp.HorizontalR esolution, bmp.Height / bmp.VerticalRes olution);
    float fScale = Math.Min( rc.Width / size.Width, rc.Height / size.Height);
    size.Width *= fScale;
    size.Height *= fScale;
    pictureBox1.Ima ge = new Bitmap(bmp, size.ToSize());
    pictureBox1.Siz eMode = PictureBoxSizeM ode.StretchImag e;

    }[/code]



    what mistake is iam doing?
    i think it is correct method. but its not working.
    please help me.
    thank u very much in advance.....
    Last edited by debasisdas; Feb 12 '08, 11:52 AM. Reason: added code=vbnet tags
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Are you getting any error ?

    Run the code the code in Debug mode and check.

    Comment

    • keerthisreenu
      New Member
      • Jan 2008
      • 5

      #3
      Originally posted by debasisdas
      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.Argument Exception:peram eter is not valid.
      at system.Drawing. Bitmap..ctor(St ream stream)

      please help me.
      thank u.

      Comment

      Working...