Retrieve byte image from Database and Display in PictureBox using C# in Windows Forms Application

Last Reply 5 days ago By raiamit

Posted 6 days ago

As I have made a table ‘[tblWatchDog]’ with columns ImageID (type int, identity type auto increment)and  ImageData (type image). Images saved successfully. While retriving I got the Error “Parameter is not valid.”

Here is my code:

  private void btnDisplay_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
                pictureBox1.Image.Dispose();

            SqlConnection con = new SqlConnection(strConn);
            SqlCommand cmd = new SqlCommand(@"SELECT ImageData  FROM tblWatchDog WHERE ImageID=1", con);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds, "tblWatchDog");
            try
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0][0] != System.DBNull.Value)
                    {
                        byte[] photo_aray = (byte[])ds.Tables[0].Rows[0][0];
                        System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
                        Image img = (Image)converter.ConvertFrom(photo_aray);
                        
                       
                     

                           pictureBox1.Image = img;// Image.FromStream(mst);
                            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                            pictureBox1.Refresh();
                      
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error",
                      MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }
        }

pl help !

Posted 6 days ago

Hi raiamit,

I have checked your code and its working correctly.

Still i have modified your code little bit. Please check the below code.

private void btnDisplay_Click(object sender, EventArgs e)
{
    if (pictureBox1.Image != null)
    {
        pictureBox1.Image.Dispose();
    }
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
    SqlCommand cmd = new SqlCommand(@"SELECT Data FROM tblFiles WHERE Id = 1", con);
    cmd.CommandType = CommandType.Text;
    if (con.State == ConnectionState.Closed)
    {
        con.Open();
    }
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    adp.Fill(ds, "tblWatchDog");
    try
    {
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (ds.Tables[0].Rows[0][0] != System.DBNull.Value)
            {
                byte[] photo_aray = (byte[])ds.Tables[0].Rows[0][0];
                System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
                Image img = (Image)converter.ConvertFrom(photo_aray);
                pictureBox1.Image = img;
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Refresh();

            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        if (con.State == ConnectionState.Open)
            con.Close();
    }
}

Output

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html?aff_id=108