b = System.Drawing.Image.FromStream(mm);//给出错误的无效参数 [英] b = System.Drawing.Image.FromStream(mm);//giving the error invalid parameter

查看:69
本文介绍了b = System.Drawing.Image.FromStream(mm);//给出错误的无效参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从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]

推荐答案

为了Pete,不要那样做!

1)不要连接字符串以构建SQL命令.它使您对意外或蓄意的SQL注入攻击敞开大门,这可能会破坏整个数据库.请改用参数化查询.
For Pete''s sake, don''t do things like that!

1) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
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);


乍一看,问题是这样的:您试图写入内存流并立即使用System.Drawing.Image.FromStream读取它.怎么可能呢?您正在尝试从流的末尾开始读取,因为流的当前位置在最后一次写入时就留在那里.

也就是说,在使用属性System.IO.MemoryStream.Position进行读取之前,您需要将内存流倒回到零位置,请参阅
From the first glance, the problem is this: you are trying to write to the memory stream and read it immediately using System.Drawing.Image.FromStream. How could it be possible? You are trying to read from the point of the end of the stream, as the current position of the stream is left there by the last write.

That said, you need to rewind the memory stream to its zero position before reading using the property System.IO.MemoryStream.Position, please see http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx[^].

Maybe there are other problem, but this is the immediate one.

—SA


这篇关于b = System.Drawing.Image.FromStream(mm);//给出错误的无效参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆