1

I have a win form with picture box and i want to store image to database so while converting image to byte to store in database I am getting the error.How to solve the error? Here is my code and I have also pointed out the line at which error (object reference not set to instance of object) occurs:

   public string ImageToBase64(Image image,
      System.Drawing.Imaging.ImageFormat format)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            // Convert Image to byte[]
            image.Save(ms, format);-------Error at this point----
            byte[] imageBytes = ms.ToArray();

            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        }
    }
Sumit Bhattarai
  • 308
  • 2
  • 19
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – ASh Jun 01 '16 at 06:35

1 Answers1

1

Instead of

   using (MemoryStream ms = new MemoryStream())
    {
        // Convert Image to byte[]
        image.Save(ms, format);-------Error at this point----
        byte[] imageBytes = ms.ToArray();
          ..
     }

It should be:

    using(var ms = new MemoryStream())
    {
        image.Save(ms, image.RawFormat);
        byte[] imageBytes = ms.ToArray();

    }

RawFormat property of image returns the format of image

EDIT 1

You could also try to use ImageConverter class

 ImageConverter converter = new ImageConverter();
 byte[] imageBytes= (byte[])converter.ConvertTo(img, typeof(byte[]));

EDIT 2

If you are saving bytearray in your database table then the columntype should be VARBINARY Else if you are saving base64string it should be VARCHAR(MAX) or VARCHAR(X)

Pushpendra
  • 1,694
  • 14
  • 27
  • Still not working.Now its showing error at image.Save(ms, image.RawFormat); this point. – Sumit Bhattarai Jun 01 '16 at 06:46
  • While you are debugging, at any point before image.Save(ms, image.RawFormat);, have you checked whether image is null? – Pushpendra Jun 01 '16 at 06:51
  • Yeah before also in my code also it was showing error at the same line.How to remove that error?? – Sumit Bhattarai Jun 01 '16 at 06:54
  • I suspect it is other function which is calling this ImageToBase64(...) is culprit. If you could post the code which is calling this I might be able to help you. – Pushpendra Jun 01 '16 at 06:57
  • conn.Sqlquery("Insert into Admission values('" + btnStudentName.Text + "','" +Convert.ToDateTime (dateTimePicker1.Text) + "','" + btnGender.Text + "','" + btnSemester.Text + "','" + btnSession.Text + "','" + btnHSSBoard.Text + "','" + ImageToBase64(pictureBox2.Image, System.Drawing.Imaging.ImageFormat.Gif) + "')"); – Sumit Bhattarai Jun 01 '16 at 07:12
  • That helped but now its showing ''String or binary data would be truncated'' at cmd.executenonquery() point. – Sumit Bhattarai Jun 01 '16 at 07:40
  • The column in which you are inserting your bytearray should be "VARBINARY" in your SQL table. – Pushpendra Jun 01 '16 at 07:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113494/discussion-between-pushpendra-and-sumit-bhattarai). – Pushpendra Jun 01 '16 at 09:45