Solved

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

Posted on 2016-09-29
9
40 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS Column mapping 5 42
Run software updates from the website 6 50
Returning multiple values with a stored procedure 9 31
Make borderless form movable by user 2 27
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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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