2
OleDbCommand and = new OleDbCommand();
c.Open();
and.Connection = c;
and.CommandText = "SELECT * FROM MaleShoes WHERE IDhere=ID ";
OleDbDataReader read = and.ExecuteReader();
while (read.Read())
{
    label6.Text = (read[1].ToString());
    textBox1.Text = (read[2].ToString());
    pictureBox1.Image = (read[3].ToString());  
}

c.Close();

我收到了这个错误:

错误 1 ​​无法将类型“字符串”隐式转换为“System.Drawing.Image”

我应该如何解决它?

我的图片在我的数据库中的第三列。

4

4 回答 4

5

如果您的数据库列包含图像文件的路径,您应该编写:

pictureBox1.Image = Image.FromFile((string)read[3]);

如果是图像数据(二进制),你应该写:

var bytes = (byte[])read[3];
using(MemoryStream ms = new MemoryStream(bytes))
{
    pictureBox1.Image = Image.FromStream(ms);
}
于 2013-03-05T07:14:02.980 回答
4

希望这能有所帮助(如果您正在存储二进制文件):

pictureBox1.Image = byteArrayToImage((byte[])read[3]);  

还有你的方法

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}
于 2013-03-05T07:13:29.473 回答
1

你也可以用这个

  byte[] imagebyte = (byte[])read[3].ToString();

  MemoryStream ms = new MemoryStream();
  ms.Write(imagebyte, 0, imagebyte.Length);
  Bitmap bmp = new Bitmap(ms);
  pictureBox1.Image = bmp;
于 2013-03-05T07:27:16.817 回答
0

你可以试试这个:

MemoryStream ms = new MemoryStream((byte[])read[1]);

pictureBox1.Image = Image.FromStream(ms);
于 2013-03-05T07:23:28.433 回答