YOUn'Me
YOUn'Me

Reputation: 107

How to read image from database

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();

I got this error:

Error 1 Cannot implicitly convert type 'string' to 'System.Drawing.Image'

How should I fix it?

My pictures are in my database on the third column.

Upvotes: 3

Views: 8553

Answers (4)

Pyromancer
Pyromancer

Reputation: 2509

you can also use this

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

  MemoryStream ms = new MemoryStream();
  ms.Write(imagebyte, 0, imagebyte.Length);
  Bitmap bmp = new Bitmap(ms);
  pictureBox1.Image = bmp;

Upvotes: 1

Vishal Suthar
Vishal Suthar

Reputation: 17194

You can try this:

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

pictureBox1.Image = Image.FromStream(ms);

Upvotes: 0

Moha Dehghan
Moha Dehghan

Reputation: 18443

If you your database column contains the path to the image file, you should write:

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

If it is the image data (binary), you should write:

var bytes = (byte[])read[3];
using(MemoryStream ms = new MemoryStream(bytes))
{
    pictureBox1.Image = Image.FromStream(ms);
}

Upvotes: 6

Alex
Alex

Reputation: 8937

Hope this help (in case you are storing a binary):

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

And your method

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

Upvotes: 4

Related Questions