Tarell Rovers
Tarell Rovers

Reputation: 1

Getting ASCII string converted and show on Picturebox

I am using this Topaz Signature Pad and it saves ASCII string upon final signature to the Database. Now i have a Problem i want to get the ASCII string from the database and convert it to an Image and show in a Picturebox Control I have this code below :

private void button4_Click(object sender, EventArgs e)
{
    string constring = @"Data Source=DESKTOP-FJBB72F\SQLEXPRESS;Initial Catalog=SignatureCapture;Integrated Security=True";
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();
        string sql = "select * from SignTable where id =@id";
        using (SqlCommand cm = new SqlCommand(sql, con))
        {
            cm.Parameters.AddWithValue("@id",textBox1.Text);
            try
            {
                using (SqlDataReader rd = cm.ExecuteReader())
                {
                    if (rd.Read())
                    {
                        // byte[] imgData = (byte[])rd["signature"];
                        byte[] imgData = Convert.FromBase64String(rd["signature"].ToString());
                        using (MemoryStream ms = new MemoryStream(imgData))
                        {
                            System.Drawing.Image image = Image.FromStream(ms);
                            //image.Save(@"C:\Users\Administrator\Desktop\UserPhoto.jpg");
                            pictureBox1.Image = image;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error: "+ex.ToString());
            }
        }
    }
}

And i get an Exception at this Line which looks thus :

enter image description here

Question is , How can i go about retrieving an ASCII and displaying on a picturebox?

Edit

SHows error at this Code .. At this line :

private void button4_Click(object sender, EventArgs e)
{
    string constring = @"Data Source=DESKTOP-FJBB72F\SQLEXPRESS;Initial Catalog=SignatureCapture;Integrated Security=True";
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();
        string sql = "select * from SignTable where id =@id";
        using (SqlCommand cm = new SqlCommand(sql, con))
        {
            cm.Parameters.AddWithValue("@id",textBox1.Text);
            try
            {
                using (SqlDataReader rd = cm.ExecuteReader())
                {
                    if (rd.Read())
                    {
                        // byte[] imgData = (byte[])rd["signature"];
                        //byte[] imgData = Convert.FromBase64String(rd["signature"].ToString());
                        byte[]imgData = Encoding.ASCII.GetBytes(rd["signature"].ToString());
                        using (MemoryStream ms = new MemoryStream(imgData))
                        {
                            System.Drawing.Image image = Image.FromStream(ms); //  Error shows at this Line <------
                            //image.Save(@"C:\Users\Administrator\Desktop\UserPhoto.jpg");
                            pictureBox1.Image = image;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Error: "+ex.ToString());
            }
        }
    }
}

Upvotes: 0

Views: 191

Answers (0)

Related Questions