title: How to convert BLOB to Image
body: <p>I have a problem in displaying the picture which is stored in my MySQL database.
  I don't know if I have stored it successfully but using this function which converts image to a blob file, here is the function:</p>
  
  <pre><code>private byte[] imageToByteArray(Image imageIn)
  {
      MemoryStream ms = new MemoryStream();
      imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
      return ms.ToArray();
  }
  </code></pre>
  
  <p>When I check my database, it says BLOB with blue highlight. Now, I would like to display the image in my picturebox. I have also a function to convert byte array to image..</p>
  
  <pre><code>private Image byteArrayToImage(byte[] byteArrayIn)
  {
      MemoryStream ms = new MemoryStream(byteArrayIn);
      ms.Position = 0;
      Image returnImage = Image.FromStream(ms);
      return returnImage;
  }
  </code></pre>
  
  <p>When I run the application, it says:</p>
  
  <blockquote>
    <p>ArgumentException was unheld, Parameter is not valid</p>
  </blockquote>
  
  <p>I have tried using this syntax:</p>
  
  <pre><code>pictureBox1.Image = byteArrayToImage(dr["img"] as byte[]);
  </code></pre>
  
  <p>Or I'm thinking if I should convert BLOB to Byte Array first? then use the function to convert the byte array into Image?<br>
  when I click the name, it should display the information, unfortunately, I'm receiving the argument exception..</p>
  
  <pre><code>int i = dataGridView1.SelectedCells[0].RowIndex;
  string firstname = dataGridView1.Rows[i].Cells[0].Value.ToString();
  string lastname = dataGridView1.Rows[i].Cells[1].Value.ToString();
  
  Connection connect = new Connection();
  MySqlConnection mySqlConnect = new MySqlConnection(connect.connString());
  mySqlConnect.Open();
  
  string s = "SELECT * FROM tbl_contacts WHERE username = '" + label1.Text + "' and (fname = '" + firstname + "' and lname = '" + lastname + "')";
  
  MySqlCommand mySQL = new MySqlCommand(s, mySqlConnect);
  mySQL.ExecuteNonQuery();
  MySqlDataReader dr = mySQL.ExecuteReader();
  
  if (dr.HasRows)
  {
      dr.Read();
      txtFname.Text = dr["fname"].ToString();
      txtLname.Text = dr["lname"].ToString();
      txtBday.Text = dr["birthday"].ToString();
      txtEmail.Text = dr["email"].ToString();
      txtMobile.Text = dr["mobile"].ToString();
      txtAddress.Text = dr["address"].ToString();
      txtNotes.Text = dr["notes"].ToString();
      pictureBox1.Image = byteArrayToImage(dr["img"] as byte[]);
  }
  </code></pre>