Display Image from Database in PictureBox Error: Parameter not invalid in Windows Application

Last Reply on May 02, 2018 03:55 AM By kalpesh

Posted on May 02, 2018 12:10 AM

Hi m trying to retrieve two images of fp from db. i get an error when i try to get it into picturebox "Parameter not valid"

datatype of image is IMAGE. but its saving like '0x53797374656D2E57696E646F77732E466F726D732E50696374757265426F782C2053697A654D6F64653A2053747265746368496D616765' in byte i guess

        private void button1_Click(object sender, EventArgs e)
        {
            try {
                con = new SqlConnection(@"Data Source=DESKTOP-JOBFB0P\SQLEXPRESS;Initial Catalog=FP;Integrated Security=True");
                con.Open();
                cmd = new SqlCommand("select * from imagestbl where id='" + textBox1.Text + "'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                textBox2.Text = dt.Rows[0][1].ToString();
                byte[] img = (byte[])dt.Rows[0][2];
                Image newimage;
                System.IO.MemoryStream ms = new System.IO.MemoryStream(img);
                newimage = Image.FromStream(ms);
                pictureBox2.Image = newimage;
                byte[] img1 = (byte[])dt.Rows[0][3];
                Image newimages;
                System.IO.MemoryStream ms1 = new System.IO.MemoryStream(img1);
                newimages = Image.FromStream(ms1);
                pictureBox2.Image = newimages;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                con.Close();
            }

please help me

Posted on May 02, 2018 03:55 AM Modified on on May 02, 2018 03:56 AM

By using below link i have created the sample which Display Image DataType column in Picture box. Reference link just display VarBinary DataType images to PictureBox.

Display Binary Image from Database in PictureBox control in Windows Application using C# and VB.Net

Also i used Northwind database in which Employee table have Photo column with Image DataType that you can download using the link given below.

Download Northwind Database

To display Image Type Column value data in Picture box

1) Convert Image DataType column value in System.Drawing.Image type Object.

2) Using MemoryStream save it into byte format.

3) Assign bytes value converted to Image to the PictureBox.

C#

private void Form1_Load(object sender, EventArgs e)
{
    string constr = @"Data Source=.; Initial Catalog=Northwind; user ID=sa; password=pass@123";
    using (SqlConnection conn = new SqlConnection(constr))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter("SELECT EmployeeID, FirstName FROM Employees", conn))
        {
            DataTable dt = new DataTable();
            sda.Fill(dt);

            DataRow row = dt.NewRow();
            row[0] = 0;
            row[1] = "Select Image";
            dt.Rows.InsertAt(row, 0);

            cmbImages.DataSource = dt;
            cmbImages.DisplayMember = "FirstName";
            cmbImages.ValueMember = "EmployeeID";
        }
    }
}

private void cmbImages_SelectionChangeCommitted(object sender, EventArgs e)
{
    string constr = @"Data Source=.; Initial Catalog=Northwind; user ID=sa; password=pass@123";
    using (SqlConnection conn = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT Photo FROM Employees WHERE EmployeeID = @Id", conn))
        {
            cmd.Parameters.AddWithValue("@Id", cmbImages.SelectedValue);
            conn.Open();      
            byte[] bytes = null;
            System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
            // Convert Image DataType column value in Image type Object 
            System.Drawing.Image img = (System.Drawing.Image)converter.ConvertFrom((byte[])cmd.ExecuteScalar());

            // using MemoryStream save it into byte format
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                bytes = ms.ToArray();
            }
            conn.Close();
            // assign bytes value converted to Image to the PictureBox
            pictureBox1.Image = Image.FromStream(new MemoryStream(bytes));
        }
    }
}
}

VB.Net 

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim constr As String = "Data Source=.; Initial Catalog=Northwind; user ID=sa; password=pass@123"
    Using conn As SqlConnection = New SqlConnection(constr)
        Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT EmployeeID, FirstName FROM Employees", conn)
            Dim dt As DataTable = New DataTable()
            sda.Fill(dt)

            Dim row As DataRow = dt.NewRow()
            row(0) = 0
            row(1) = "Select Image"
            dt.Rows.InsertAt(row, 0)

            cmbImages.DataSource = dt
            cmbImages.DisplayMember = "FirstName"
            cmbImages.ValueMember = "EmployeeID"
        End Using
    End Using
End Sub

Private Sub cmbImages_SelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs) Handles cmbImages.SelectionChangeCommitted
    Dim constr As String = "Data Source=.; Initial Catalog=Northwind; user ID=sa; password=pass@123"
    Using conn As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT Photo FROM Employees WHERE EmployeeID = @Id", conn)
            cmd.Parameters.AddWithValue("@Id", cmbImages.SelectedValue)
            conn.Open()
            Dim bytes As Byte() = Nothing
            Dim converter As System.Drawing.ImageConverter = New System.Drawing.ImageConverter()
            Dim img As System.Drawing.Image = CType(converter.ConvertFrom(CType(cmd.ExecuteScalar(), Byte())), System.Drawing.Image)
            Using ms As MemoryStream = New MemoryStream()
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
                bytes = ms.ToArray()
            End Using

            conn.Close()
            pictureBox1.Image = Image.FromStream(New MemoryStream(bytes))
        End Using
    End Using
End Sub

Screenshot