如何从数据库中显示图片框中的图像 [英] how yo display image in picturebox from database

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

问题描述

我使用以下代码



 SqlConnection con =  new  SqlConnection ( 数据源= SHRIKESH-PC\\SQLEXPRESS;初始目录=窗口;集成安全性=真); 
con.Open();
SqlCommand cmd = new SqlCommand( select来自test的myphoto,其中id =' + textBox2.Text + ',con) ;
cmd.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0
{
MemoryStream ms2 = new MemoryStream(( byte [])dt.Rows [ 0 ] [ Myphoto]);
pictureBox1.Image = Image.FromStream(ms2);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Refresh();
}
else
{
MessageBox.Show( 请选择ImageID);
}

它给出了一个错误=

参数ID在pictureBox1.Image = Image.FromStream(ms2);  

解决方案

参考这个经典链接:为什么我得到参数无效。我从数据库中读取图像时出现异常?


I use following code

SqlConnection con = new SqlConnection("Data Source=SHRIKESH-PC\\SQLEXPRESS;Initial Catalog=windows;Integrated Security=True");
           con.Open();
           SqlCommand cmd = new SqlCommand("select myphoto from test where id='"+textBox2.Text+"'", con);
           cmd.ExecuteNonQuery();
           SqlDataAdapter sda = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           sda.Fill(dt);
           if (dt.Rows.Count > 0)
           {
               MemoryStream ms2 = new MemoryStream((byte[])dt.Rows[0]["Myphoto"]);
               pictureBox1.Image = Image.FromStream(ms2);
               pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
               pictureBox1.Refresh();
           }
           else
           {
               MessageBox.Show("Please Select ImageID");
           }

it gives an error =

parameter id not valid at pictureBox1.Image = Image.FromStream(ms2);

解决方案

Refer this classic link : Why do I get a "Parameter is not valid." exception when I read an image from my database?


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

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