如何使用ImageConverter从数据库检索图像到图片框?

时间:2016-05-25 14:35:01

标签: c# database picturebox

Image img = PictureBox1.Image;
byte[] arr;
ImageConverter converter = new ImageConverter();
arr = (byte[])converter.ConvertTo(img, typeof(byte[]));

SqlCommand cmd = new SqlCommand("insert into tblWerknemers2 (Naam,Voornaam,Foto) values(@Naam,@Voornaam@Foto)", con);
cmd.Parameters.AddWithValue("@Naam", txtNaam.Text);
cmd.Parameters.AddWithValue("@Voornaam", txtVoornaam.Text);
cmd.Parameters.AddWithValue("@Foto", arr);

这里我将图像插入数据库,但问题是我不知道如何检索它。以下是我尝试这样做的方法。

SqlDataReader read = command.ExecuteReader();

while (read.Read())
{
    txtNaam.Text = (read["Naam"].ToString());
    txtVoornaam.Text = (read["Voornaam"].ToString());  
    arr = (byte[])read["Foto"];              
}
read.Close();

1 个答案:

答案 0 :(得分:0)

byte[] arr = (byte[])read["Foto"];
if (arr.Length > 0)
{
    MemoryStream mem = new MemoryStream(arr);
    PictureBox1.Image = Image.FromStream(mem);
}