Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I need to retrieve the longblob of a picture from mysql.
So first I insert it in the database with this:
C#
MemoryStream ms = new MemoryStream();
                  //  PIC_Image.Image.Save(ms, PIC_Image.Image.RawFormat);
                    byte[] img = ms.ToArray();

                    string q = "insert into tb_produits (Image,Designation,Reference,CodeEAN,ChaineEAN,Quantite,PrixReviens,PrixVente) values('" + img+ "')";

Here is to retrieve the picture :
C#
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);
            }


What I have tried:

I tried to retrieve but I got an error:
Parameter is not valid in this line " Bitmap bm = new Bitmap(mStream, false);"
Posted
Updated 27-Apr-18 9:23am

1 solution

C#
byte[] img = (byte[])dt.Rows[0]["Image"];

Here you should try put like this:
C#
byte[] img = (byte[])dt.Rows[0]["put the number of column start from zero, in this case I'll say: " 1]


I think this should fix it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900