0

I am retrieving the image from a SQL Server database into a PictureBox in a C# Windows application. The image is a form which I don't know.

I tried this code but it throws a "parameter invalid" exception, and I also go through many answer related to this but didn't work any.

My code:

  SqlConnection sql_con  = new SqlConnection();
  sql_con.ConnectionString = @"Server=abc-14;Database=Abcstudent;User Id=sa; Password=abc;";

  if (sql_con.State == ConnectionState.Closed) 
      sql_con.Open();

  try
  {
      string query = "Select grno, PICStudent from GENREG where grno = 1";                        
      SqlCommand sql_cmd = new SqlCommand(query, sql_con);

      sql_cmd.ExecuteNonQuery();

      SqlDataAdapter sql_adp = new SqlDataAdapter(sql_cmd);               
      sql_adp.Fill(sql_ds);    
  }
  catch(Exception ex)
  {
  }

  // code for retrieving image to picture box 
  try
  {
       DataRow myRow;
       byte[] MyData = new byte[0];
       myRow = sql_ds.Tables[0].Rows[0];

       MyData = (byte[])myRow["PICStudent"];
       MemoryStream stream = new MemoryStream(MyData);
       pictureBox1.Image = Image.FromStream(stream);  // getting error here
  }
  catch()
  {
  }

Database Image enter image description here

2
  • In which line of code you are getting the error? Commented Jan 7, 2017 at 5:14
  • I update question with line of error in comments Commented Jan 7, 2017 at 6:21

2 Answers 2

1
 SqlConnection con = new SqlConnection("Data Source=SERVERNAME;Initial Catalog=DATABASENAME;Integrated Security=SSPI;");
        con.Open();
        SqlCommand cmd = new SqlCommand("select picbox from picture1 where id=1", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["picbox"]);
            pictureBox1.Image = new Bitmap(ms);
        }
Sign up to request clarification or add additional context in comments.

4 Comments

thank you @vinoth but same error : parameter invalid
in which line@Mamta
pictureBox1.Image = new Bitmap(ms) // Parameter Invalid
I didn't know the type of image in database like jpg , png etc but datatypes is Image in table
1

Insead of using Image.FromStream, did you try using ImageConverter?

Sample come of using ImageConverter looks as following.

byte[] filedata = (byte[])myRow["PICStudent"];
ImageConverter imageConverter = new System.Drawing.ImageConverter();
System.Drawing.Image image = imageConverter.ConvertFrom(fileData) as System.Drawing.Image;
image.Save(imageFullPath, System.Drawing.Imaging.ImageFormat.Jpeg);

This should resolve your issue.

Thanks and regards, Chetan Ranpariya

1 Comment

thank you for answer @Chetan but getting error "Parameter Invalid " in imageConverter.ConvertFrom(fileData) as System.Drawing.Image ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.