我正在使用此Topaz签名板,它将最终签名时的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并显示在图片框上?
修改
SHows在此代码上的错误..在此行:
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());
}
}
}
}