在图片框中显示数据库中的图像 [英] display image in picturebox from database

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

问题描述

如何在数据库的PictureBox中显示图像.我正在使用.NET窗口应用程序.我为此问题编写了代码,但出现错误.
请解决我的问题或告诉我其他解决方案.

How to display image in PictureBox from database. I''m working .NET window application. I wrote code for this problem but I got error.
Please solve my problem or tell me other solution.

        private void button4_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd = new SqlCommand("select * from tblImage where ID=2", con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                byte[] b = new byte[0];
                b = (Byte[])(dr["ImgUrl"]);
                MemoryStream ms = new MemoryStream(b);
                pictureBox1.Image = Image.FromStream(ms);
                //pictureBox1.Image = Convert.ToSByte(dr["Url"].ToString());

}
            con.Close();
        }


I took two column in table.firstone path type nvarchar(max) and second one ImgUrl type image.

推荐答案

表中的列名称似乎说嘿,我是图片的url,这就是他们为什么称我为ImgUrl的原因".另一方面,查看代码似乎意味着图像本身存储在数据库中,而不是图像的某些URL.

那么ImgUrl列是什么类型?
就像Umair所说的那样,请在您的问题中发布错误消息.

问候,
Manfred
The column name in your table seems to say "Hey I''m the url to an image that''s why they called me ImgUrl". Looking at the code on the other hand seems to imply that the image itself is stored in the database and not some url to an image.

So what type is column ImgUrl?
And as Umair already said please post the error message in your question.

Regards,
Manfred


所有是否都检查是否正确插入了数据?如果可能,请发布插入代码..

http://www.eggheadcafe.com/community/aspnet/14/10041370/how-to-display-image-in-picturebox.aspx [ http://support.microsoft.com/kb/317701 [
All so check correctly data inserted?. Post the insertion code if possible..

http://www.eggheadcafe.com/community/aspnet/14/10041370/how-to-display-image-in-picturebox.aspx[^]

http://support.microsoft.com/kb/317701[^]


OleDbConnection conn = new OleDbConnection(connectionString);
           OleDbCommand SQLCommand = new OleDbCommand();
           conn.Open();
           string getpwd = textBox3.Text.ToString();
           string query2 = "select * from Table1 where Filename= '" + getpwd + "'";
           Console.Write(query2);
           OleDbCommand s = new OleDbCommand(query2, conn);
           OleDbDataReader bReader = s.ExecuteReader();
           if (textBox3.Text == "")
           {
               MessageBox.Show("Please Enter the Image name to retrive!!");
            }

           else if (bReader.Read())
           {
               byte[] b = new byte[0];
               b = (Byte[])(bReader["imgname"]);
               MemoryStream ms = new MemoryStream(b);
               pictureBox1.Image = Image.FromStream(ms);
               //pictureBox1.Image = Convert.ToSByte(dr["Url"].ToString());

           }


           else
           {
               MessageBox.Show("Please Enter Valid Image name!!!");
           }


           conn.Close();


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

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