0

我正在使用这个黄玉签名板,它在最终签名时将 ASCII 字符串保存到数据库。现在我有一个问题我想从数据库中获取 ASCII 字符串并将其转换为图像并显示在 Picturebox 控件中我有以下代码:

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());
            }
        }
    }
}

我在这条线上得到一个例外,看起来像这样:

在此处输入图像描述

问题是,我如何才能检索 ASCII 并在图片框上显示?

编辑

在此代码中显示错误.. 在此行:

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());
            }
        }
    }
}
4

0 回答 0