Solved

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

Posted on 2016-09-29
9
20 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
Comment Utility
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
 

Author Comment

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

Expert Comment

by:AndyAinscow
Comment Utility
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
 

Author Comment

by:SniperCode Sheva
Comment Utility
I forgot to edit this PIC_Image.Image.Save(ms, PIC_Image.Image.RawFormat);
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
You had that in your code but commented out.  Please post your code that you use - without editing it
0
 

Author Comment

by:SniperCode Sheva
Comment Utility
 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
Comment Utility
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
 

Accepted Solution

by:
SniperCode Sheva earned 0 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now