title: Insert PictureBox Image into Sql Server database
body: <p>Im trying to insert into my table some image from <code>picturebox</code>:</p>
  
  <pre><code>  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();
  </code></pre>
  
  <p>I get the following result in database:</p>
  
  <pre><code>ID  Image
  6   0x53797374656D2E427974655B5D
  </code></pre>
  
  <p>However when I insert some image using <code>SQL script</code>:</p>
  
  <pre><code>insert into ImagesTable (Image) 
  SELECT BulkColumn 
  FROM Openrowset( Bulk 'C:\pinguins.jpg', Single_Blob) as img
  </code></pre>
  
  <p>Then inserted data looks like this:</p>
  
  <pre><code>ID  Image
  4   0xFFD8FFE000104A464946000102010[.....]
  </code></pre>
  
  <p>Here binary data is much much longer. </p>
  
  <p>When I retrieve this image from database back into <code>picturebox</code>, it shows up correctly:</p>
  
  <pre><code>           command.CommandText = "SELECT Image FROM ImagesTable where ID = 4";
  
              byte[] image = (byte[])command.ExecuteScalar();
              MemoryStream ms1 = new MemoryStream(image);
              pictureBox2.Image = Bitmap.FromStream(ms1);  
  </code></pre>
  
  <p>But I get error when retrieving image with <code>ID = 6</code> (loaded from <code>pictureBox</code>).</p>
  
  <pre><code>ArgumentException: Parameter is not valid.
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>I'd appreciate any advice.</p>