问题描述
我正在尝试从sqlserver获取签名图像,如....
hi guys i am trying to get the signature image from sqlserver like....
private void button1_Click(object sender, EventArgs e)
{
textBox5.Text = "";
textBox4.Text = "";
textBox3.Text = "";
textBox2.Text = "";
DataConnection data = new DataConnection();
SqlConnection con;
con = data.GetConnection();
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select*from tblInbound where consignmentNo=('" + textBox1.Text + "')";
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
textBox2.Text = dr[7].ToString();
textBox5.Text = dr[12].ToString();
textBox3.Text = dr[10].ToString();
textBox4.Text = dr[11].ToString();
//textBox6.Text = dr[46].ToString();
Byte[] imagedata = new Byte[0];
imagedata = (byte[])dr[45];
System.Drawing.Image b = null;
MemoryStream mm = new MemoryStream(imagedata);
mm.Write(imagedata, 0, imagedata.Length);
//pictureBox1.Image = Image.FromStream(mm);
b = System.Drawing.Image.FromStream(mm);//giving the error invalid parameter
但是它在b = System.Drawing.Image.FromStream(mm)处给出了错误;无效的参数..您能告诉我这段代码有什么问题吗?.
谢谢...
but its giving the error at b = System.Drawing.Image.FromStream(mm); invalid parameter..can u please tell me whats wrong with this code..
thanks...
//using (MemoryStream mm = new MemoryStream())
//{
// System.Drawing.Image b;
// Bitmap bitmap = null;
// mm.Write(imagedata, 0, imagedata.Length);
// b = System.Drawing.Image.FromStream(mm);
// pictureBox1.Image = b;
// }
}
[edit]已添加代码块,将我的内容视为纯文本..."选项已禁用-OriginalGriff [/edit]
[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
推荐答案
cmd.CommandText = "select*from tblInbound where consignmentNo=@CN";
cmd.Parameters.AddWithValue("@CN", textBox1.Text);
2)不要使用SELECT * FROM ...,这会浪费带宽和内存,尤其是当其中一个字段是图像时.仅列出您将要使用的字段.
3)永远不要将SqlDateReader用作带有数字索引的数组,除非字段列表非常非常短且特别指定.他们中的45个或更多只是愚蠢的.请改用字段名称-它使您的代码更具可读性和可靠性.如果有人下个月决定不需要第二列并将其从您的数据库中删除,您的代码会怎样?
解决该问题,然后解决您的问题-这就是您添加了不需要的单行代码...
...摆脱这条线:
2) Don''t use SELECT * FROM... it wastes bandwidth, and memory, particularly when one of the fields is an image. List only the fields you are going to use.
3) Don''t ever use SqlDateReader as an array with a numeric index, unless the list of fields is very, very short, and specifically specified. 45 of them or more is just plain stupid. Use the name of the field instead - it makes your code more readable, and reliable. What happens to your code if someone decides next month that column 2 is not needed and deletes it from your database?
Fix that lot, and then fix your problem - which is that you have added a single line of code you do not need...
...get rid of the line:
mm.Write(imagedata, 0, imagedata.Length);
这篇关于b = System.Drawing.Image.FromStream(mm);//给出错误的无效参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!