从数据库显示图像时图片框中出现错误 [英] error in picture box when display image from database

查看:72
本文介绍了从数据库显示图像时图片框中出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中使用下面的编码来设置图像,它给了错误,请帮忙.. !!

byte[] imageSource = ((byte[])dtCompany.Rows[0]["CompanyLogo"]);
                            MessageBox.Show(Convert.ToString(imageSource));
                            Bitmap image;
                            using (MemoryStream stream = new MemoryStream(imageSource))
                            {
                               image = new Bitmap(stream);
                            }
                            pbCompanyLogo.Image = image;

推荐答案

通常,您遇到的问题是参数无效".表示数据库中的数据不是有效的图像.通常,问题实际上是在将映像加载到数据库中时引起的,特别是如果通过连接字符串以形成SQL命令来保存映像:
Normally, the problem you get is "Parameter is not valid." which indicates that the data in teh database is not a valid image. Often the problem is actually caused when the image is loaded into the database, particularly if the image is saved by concatenating strings to form an SQL command:
string sql = "INSERT INTO myTable (imageData) VALUES ('" + myImage + "')";

实际上会生成一个sql字符串:

Actually generates an sql string:

INSERT INTO myTable (imageData) VALUES ('System.Drawing.Bitmap')

不会引发错误,但也不会存储实际的图像数据.

检查数据库,如果是这种情况,请使用参数化查询来加载数据库而不是字符串连接(当然,您应该这样做).

Which won''t throw an error, but also doesn''t store the actual image data.

Check your DB, and if this is the case use parametrized queries to load the DB instead of string concatenation (you should be doing that anyway as a matter of course).


嘿,
我希望这是您正在寻找的解决方案

使用此代码将图片插入数据库

Hey,
I wish that this is the solution that you are looking for

use this code to Insert A pic into DB

//We are using SQL express.
            //My database name is "PictureDb".
            SqlConnection con = new SqlConnection
                               ("Server=.;database=PictureDb;integrated security=true");
            //I have used a table named "tblUsers" and fill the fields
            SqlCommand com = new SqlCommand("insert into tblUsers
                            (fldCode,fldPic) values(1,@Pic)", con);

            //In here, I have to save the picturebox image to tblUsers
            //because you were not able to send the PictureBox1.Image
            //to field "fldPic" that is of kind
            //"image" in SQL SERVER EXPRESS, then you should do something else
            // and that is converting
            //your picture box image to an array of bytes and then sending
            //that array to database.
            //Below, we have used a stream which will be used to
            //contain our picturebox image bytes.
            MemoryStream stream=new MemoryStream();
            //through the instruction below, we save the
            //image to byte in the object "stream".
            pictureBox1.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);

            //Below is the most important part, actually you are
            //transferring the bytes of the array
            //to the pic which is also of kind byte[]
            byte[] pic=stream.ToArray();

            com.Parameters.AddWithValue("@Pic", pic);
            try
            {
                con.Open();
                com.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }




和此代码可从DB接收图片




And This Code To recieve the picture from DB

SqlConnection connect = new SqlConnection
                             ("Server=.;database=PictureDb;integrated security=true");
            SqlCommand command = new SqlCommand
                                ("select fldPic from tblUsers where fldCode=1", connect);
            //for retrieving the image field in SQL SERVER EXPRESS
            //Database you should first bring
            //that image in DataList or DataTable
            //then add the content to the byte[] array.
            //That's ALL!
            SqlDataAdapter dp = new SqlDataAdapter(command);
            DataSet ds = new DataSet("MyImages");

            byte[] MyData = new byte[0];

            dp.Fill(ds, "MyImages");
            DataRow myRow;
            myRow = ds.Tables["MyImages"].Rows[0];

            MyData = (byte[])myRow["fldPic"];

            MemoryStream stream = new MemoryStream(MyData);
            //With the code below, you are in fact converting the byte array of image
            //to the real image.
            pictureBox2.Image = Image.FromStream(stream);




问候,
A.Mandour




Regards,
A.Mandour


这篇关于从数据库显示图像时图片框中出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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