Solved

[C#]How to retrieve LONGBLOB image from mysql in picturebox

Posted on 2016-09-29
9
34 Views
Last Modified: 2016-11-09
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);"
0
Comment
Question by:SniperCode Sheva
  • 4
  • 4
9 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41822004
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.
0
 
LVL 1

Author Comment

by:SniperCode Sheva
ID: 41822033
It is not empty because in the database I see this : "[BLOB - 13 o]"
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41822144
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+ "')";
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:SniperCode Sheva
ID: 41822148
I forgot to edit this PIC_Image.Image.Save(ms, PIC_Image.Image.RawFormat);
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41822155
You had that in your code but commented out.  Please post your code that you use - without editing it
0
 
LVL 1

Author Comment

by:SniperCode Sheva
ID: 41822185
 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
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41822230
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+ "')";
0
 
LVL 1

Accepted Solution

by:
SniperCode Sheva earned 0 total points
ID: 41823358
Hello, I found a solution to add the picture with your code now I need to check if the image is empty in the database ....
Here is what I did;
try
            {

                
                MySqlDataAdapter sda = new MySqlDataAdapter("SELECT * FROM tb_produits", MyConnexion);
                sda.Fill(dt);

              
              
                    
                        object o = (byte[])dt.Rows[0]["Image"];
                        if (o is System.DBNull)
                    {
                        PIC_Image.Image = Properties.Resources.Aa;
                        PIC_Image.SizeMode = PictureBoxSizeMode.Zoom;
                        PIC_Image.BorderStyle = BorderStyle.FixedSingle;
                        PIC_Image.Refresh();
                    }
                    else
                    {
                        byte[] img = (byte[])dt.Rows[count]["Image"];

                        MemoryStream ms = new MemoryStream(img);
                        PIC_Image.Image = Bitmap.FromStream(ms);
                        PIC_Image.SizeMode = PictureBoxSizeMode.Zoom;
                        PIC_Image.BorderStyle = BorderStyle.FixedSingle;
                        PIC_Image.Refresh();

                    }
}

Open in new window

But the picture that I gave when it si empty doesn't appear...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question