Solved

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

Posted on 2016-09-29
9
30 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
 

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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
 

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
 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Batch file or script with password 22 44
Footer for each row on Gridview 2 21
Help with adding DLL file in Windows project 20 31
location of a form 2 14
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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