Link to home
Start Free TrialLog in
Avatar of SniperCode Sheva
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:
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+ "')";

Open in new window

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);
            }

Open in new window

I tried to retrieve but I got an error:
Parameter is not valid in this line " Bitmap bm = new Bitmap(mStream, false);"
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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+ "')";

Open in new window

img is empty when you save it.  Hence your failure when you retrieve it.
Avatar of SniperCode Sheva
SniperCode Sheva

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+ "')";
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
 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+ "')";

Open in new window

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+ "')";
ASKER CERTIFIED SOLUTION
Avatar of SniperCode Sheva
SniperCode Sheva

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial