Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to store a image from a picture box (frontPictureBox) to a SQL DB table called Music. I realize I have to convert it to a Byte Array. But when I try retrieving it from the database I end up with a black background in the pictureBox.

Here is the code:

C#
SqlConnection con = new SqlConnection("Server=.;database=CollectionDb;integrated security=true");
           
            SqlCommand com = new SqlCommand("insert into tblMusic(fldCode,fldPic) values(1,@Pic)", con);

            
            MemoryStream stream=new MemoryStream();
            
            frontPictureBox.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);

            
            byte[] pic=stream.ToArray();

            com.Parameters.AddWithValue("@Pic", pic);
            try
            {
                con.Open();
                com.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }


Now the program is throwing a error.
Posted
Updated 3-Jul-15 5:55am
v2
Comments
PIEBALDconsult 3-Jul-15 11:59am    
Might it be that the database column has the wrong datatype?
The cose you show seems OK, but what about the code to retrieve from the database?

1 solution

JPEG doesn't support transparency. So you need to fill the background with some color or save it as PNG instead of JPEG.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900