After quite some time of trying and looking arround here for some answers, i am unable to load an image from MySQL.
Basicly, a user choses a picture from and OpenFileDialog
which is then loaded in a PictureBox
(this works fine).
Then the user clicks a button which will save the picture in the db by converting it to a byte[]
.
Finally, when I try to load the picture in another PictureBox
, it's all black.
When adding the picture to the db:
public void PictureToDB(Image img)
{
MemoryStream tmpStream = new MemoryStream();
img.Save(tmpStream, ImageFormat.Png);
tmpStream.Seek(0, SeekOrigin.Begin);
byte[] imgBytes = new byte[5000];
tmpStream.Read(imgBytes, 0, 5000);
string query = "Update TABLE Set IMG = @imgBytes";
MySqlParameter param = new MySqlParameter("@img", imgBytes);
//Skipping connection to db and all... it works
}
Getting picture from db:
public void DBToPicture()
{
string query = "Select IMG From TABLE Where ...";
//Skipping Command lines ...
MySqlDataReader reader = command.ExecuteReader();
DataTable myDT.Load(reader);
Byte[] data = new Byte[0];
data = (Byte[])(myDT.Rows[0]["PHOTOLOISANT"]);
MemoryStream mem = new MemoryStream(data);
MyPictureBox.Image = Image.FromStream(mem);
}
More info:
- the type in MySQL is
varbinary(8000)
and contains this "89504e470d0a1a0a0000000d4948445200000280000001e00802000000bab34bb3000000017352474200aece1ce90000000467414d41002e2e2e" after theupdate
- the image is converted to bitmap when it is loaded in the first
PictureBox
.