locked
Parameter is not valid RRS feed

  • Question

  • Hi, i have this application that will display an image in a picture box and I am getting an error "Parameter is not valid".

    The image to be displayed is from SQL SERVER 2008 database and the image datatype is varbinary(max). There are only few images that throw "Parameter is not valid" error.

    Here is the code snippet:

    byte[] getImage = (byte[])dr["image"];
    MemoryStream ms = new MemoryStream(getImage);

    object objImg = new Bitmap(Bitmap.FromStream(ms));  //  error occurs here
    pictureBox1.Image = new Bitmap(Bitmap.FromStream(ms));

     

    I checked different forums but no one can tell how to fix this error. Hope you can help me.

    Thanks in advance

     

    Cyrilla

     

     

    Tuesday, June 28, 2011 3:06 AM

Answers

  •             DataSet tds = new DataSet();
                SqlConnection cn = new SqlConnection("connection string");
                SqlCommand cmd = new SqlCommand("select * from [table name]", cn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(tds, "image");
                byte[] getImage = new byte[0];
                foreach (DataRow dr in tds.Tables [0].Rows)
                {
                    getImage = (byte[])dr["imagePath"]; //column name //from your codes
                }
                MemoryStream ms = new MemoryStream(getImage);   //from your codes
                object objImg = new Bitmap(Bitmap.FromStream(ms));  //from your codes
                pictureBox1.Image = new Bitmap(Bitmap.FromStream(ms)); //from your codes
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful
    Tuesday, June 28, 2011 5:29 AM
  • check that if all the images you are having in db are of .bmp type. There may be a case where the byte format of the image stored not as per Bitmap .bmp file standards causing an error. I think it wld be better to first load image, then save it in your case want to show up in picture box.

    MemoryStream ms = new MemoryStream(getImage);
    Bitmap
     bmp = (Bitmap)Image.FromStream(ms);             bmp.Save(@"C:\file.jpg");             bmp.Dispose();

    Mark Answered, if it solves your query
    Rohit Arora
    Tuesday, June 28, 2011 8:27 AM

All replies

  •             DataSet tds = new DataSet();
                SqlConnection cn = new SqlConnection("connection string");
                SqlCommand cmd = new SqlCommand("select * from [table name]", cn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(tds, "image");
                byte[] getImage = new byte[0];
                foreach (DataRow dr in tds.Tables [0].Rows)
                {
                    getImage = (byte[])dr["imagePath"]; //column name //from your codes
                }
                MemoryStream ms = new MemoryStream(getImage);   //from your codes
                object objImg = new Bitmap(Bitmap.FromStream(ms));  //from your codes
                pictureBox1.Image = new Bitmap(Bitmap.FromStream(ms)); //from your codes
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful
    Tuesday, June 28, 2011 5:29 AM
  • check that if all the images you are having in db are of .bmp type. There may be a case where the byte format of the image stored not as per Bitmap .bmp file standards causing an error. I think it wld be better to first load image, then save it in your case want to show up in picture box.

    MemoryStream ms = new MemoryStream(getImage);
    Bitmap
     bmp = (Bitmap)Image.FromStream(ms);             bmp.Save(@"C:\file.jpg");             bmp.Dispose();

    Mark Answered, if it solves your query
    Rohit Arora
    Tuesday, June 28, 2011 8:27 AM