Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read image from database

Tags:

c#

sql

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.

like image 842
YOUn'Me Avatar asked Mar 27 '26 11:03

YOUn'Me


1 Answers

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);
}
like image 100
Mohammad Dehghan Avatar answered Mar 30 '26 00:03

Mohammad Dehghan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!