Insert PictureBox Image into Sql Server database

54,443

write this way:]

Image img = Image.FromFile(@"C:\Lenna.jpg");
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    arr =  ms.ToArray();
}

or

 Image img = picturebox1.Image();
    byte[] arr;
 ImageConverter converter = new ImageConverter();
   arr=(byte[])converter.ConvertTo(img, typeof(byte[]));

command.CommandText = "INSERT INTO ImagesTable (Image) VALUES('" + arr + "')";
  command.CommandType = CommandType.Text;
  command.ExecuteNonQuery();
Share:
54,443
Gyuzal R
Author by

Gyuzal R

Updated on January 31, 2020

Comments

  • Gyuzal R
    Gyuzal R almost 4 years

    Im trying to insert into my table some image from picturebox:

      MemoryStream ms = new MemoryStream();
      pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
      byte[] photo = new byte[ms.Length];
      ms.Position = 0;
      ms.Read(photo, 0, photo.Length);
    
      command.CommandText = "INSERT INTO ImagesTable (Image) VALUES('" + photo + "')";
      command.CommandType = CommandType.Text;
      command.ExecuteNonQuery();
    

    I get the following result in database:

    ID  Image
    6   0x53797374656D2E427974655B5D
    

    However when I insert some image using SQL script:

    insert into ImagesTable (Image) 
    SELECT BulkColumn 
    FROM Openrowset( Bulk 'C:\pinguins.jpg', Single_Blob) as img
    

    Then inserted data looks like this:

    ID  Image
    4   0xFFD8FFE000104A464946000102010[.....]
    

    Here binary data is much much longer.

    When I retrieve this image from database back into picturebox, it shows up correctly:

               command.CommandText = "SELECT Image FROM ImagesTable where ID = 4";
    
                byte[] image = (byte[])command.ExecuteScalar();
                MemoryStream ms1 = new MemoryStream(image);
                pictureBox2.Image = Bitmap.FromStream(ms1);  
    

    But I get error when retrieving image with ID = 6 (loaded from pictureBox).

    ArgumentException: Parameter is not valid.
    

    What am I doing wrong?

    I'd appreciate any advice.

  • Gyuzal R
    Gyuzal R almost 10 years
    Thank you, but is there a way to store directly from PictureBox? Not from physical path? Or there is a case when I need to store picture from Bitmap.
  • pankeel
    pankeel almost 10 years
    write this: Image img = PictureBox1.Image();
  • Gyuzal R
    Gyuzal R almost 10 years
    Thanks for your effort, I found a solution. codeproject.com/Articles/25956/…
  • Andrew Barber
    Andrew Barber over 9 years
    Please consider including some information about your answer, rather than simply posting code. We try to provide not just 'fixes', but help people learn. You should explain what was wrong in the original code, what you did differently, and why your change(s) worked.
  • Admin
    Admin over 5 years
    Edit your code : values(' " + textBox1.Text + " ' ,'" you forgot the single quotes for textBox1.text
  • Mad Myche
    Mad Myche over 4 years
    Code like this invites SQL Injection- NEVER piece a command together from strings and direct user input; ALWAYS use parameters