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);"
Parameter is not valid in this line " Bitmap bm = new Bitmap(mStream, false);"
Midhun TpPosted Sep 29, 2016, 9:30 PM
Please follow below thread-
http://stackoverflow.com/questions/13913353/convert-binary-to-bitmap-using-memory-stream