title: System.NullReferenceException: Object reference not set to an instance of an object
body: <p>In this code i wanted to save the image in picturebox1 in database using linqtosql ..... but getting some exceptions in converting a image to byte array</p>
  
  <p>Exception is  "System.NullReferenceException: Object reference not set to an instance of an object."</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
  
  {
      DataClasses1DataContext dc = new DataClasses1DataContext();
      try
      {
          string signname = textBox1.Text;
          string imageurl = textBox2.Text;
          pictureBox1.ImageLocation = imageurl;
       //   byte[] file_byte = new byte[1000];
         // Image newimage = new Image(pictureBox1.Image);
      ///Error comes here
       byte[] file_byte = ImageToByteArray(pictureBox1.Image);
          System.Data.Linq.Binary file_binary = new                                                           System.Data.Linq.Binary(file_byte);
  
  
          Sign_Table obj = new Sign_Table()
          {
              Sign_Name = signname,
              Sign_Image = file_binary,
  
          };
          dc.Sign_Tables.InsertOnSubmit(obj);
  
      }
      finally
      {
          dc.SubmitChanges();
      }
  
  }
  private byte[] ImageToByteArray(Image imageIn )
  {
      using (MemoryStream ms = new MemoryStream())
      {
  
             // Error comes here
          imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
          return ms.ToArray();
      }
  
  }
  </code></pre>