Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-09-29
9
Medium Priority
?
67 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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