SniperCode Sheva
asked on
[C#]How to retrieve LONGBLOB image from mysql in picturebox
Hello,
I need to retrieve the longblob of a picture from mysql.
So first I insert it in the database with this:
Parameter is not valid in this line " Bitmap bm = new Bitmap(mStream, false);"
I need to retrieve the longblob of a picture from mysql.
So first I insert it in the database with this:
MemoryStream ms = new MemoryStream();
// PIC_Image.Image.Save(ms, PIC_Image.Image.RawFormat);
byte[] img = ms.ToArray();
string q = "insert into tb_produits (Image) values('" + img+ "')";
Here is to retrieve the picture :public static Bitmap ByteToImage(byte[] blob)
{
MemoryStream mStream = new MemoryStream();
byte[] pData = blob;
mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
Bitmap bm = new Bitmap(mStream, false);
mStream.Dispose();
return bm;
}
MySqlDataAdapter sda = new MySqlDataAdapter("SELECT * FROM tb_produits WHERE Designation='" + Designation + "'and Reference='" + Reference + "'", MyConnexion);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count == 1)
{
byte[] img = (byte[])dt.Rows[0]["Image"];
PIC_Image.Image = ByteToImage(img);
}
I tried to retrieve but I got an error:Parameter is not valid in this line " Bitmap bm = new Bitmap(mStream, false);"
ASKER
It is not empty because in the database I see this : "[BLOB - 13 o]"
This is your code:
new memory stream with nothing in it
MemoryStream ms = new MemoryStream();
convert empty memory stream to empty byte array = no picure
byte[] img = ms.ToArray();
insert the empty byte array into the database
string q = "insert into tb_produits (Image) values('" + img+ "')";
new memory stream with nothing in it
MemoryStream ms = new MemoryStream();
convert empty memory stream to empty byte array = no picure
byte[] img = ms.ToArray();
insert the empty byte array into the database
string q = "insert into tb_produits (Image) values('" + img+ "')";
ASKER
I forgot to edit this PIC_Image.Image.Save(ms, PIC_Image.Image.RawFormat) ;
You had that in your code but commented out. Please post your code that you use - without editing it
ASKER
MemoryStream ms = new MemoryStream();
PIC_Image.Image.Save(ms, PIC_Image.Image.RawFormat);
byte[] img = ms.GetBuffer();
string q = "insert into tb_produits (Image) values('" + img+ "')";
Image is of type Blob
OK. Lets try to see which bits of code work by modifying things slightly.
MemoryStream ms = new MemoryStream();
PIC_Image.Image.Save(ms, PIC_Image.Image.RawFormat) ;
byte[] img = ms.GetBuffer();
See if this line here works - converting your image to a blob and then back to the bitmap without using the database.
Bitmap bm = ByteToImage(img);
string q = "insert into tb_produits (Image) values('" + img+ "')";
MemoryStream ms = new MemoryStream();
PIC_Image.Image.Save(ms, PIC_Image.Image.RawFormat)
byte[] img = ms.GetBuffer();
See if this line here works - converting your image to a blob and then back to the bitmap without using the database.
Bitmap bm = ByteToImage(img);
string q = "insert into tb_produits (Image) values('" + img+ "')";
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
img is empty when you save it. Hence your failure when you retrieve it.