Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I am trying to convert image in Asp.Net Image control to byte to save the image in image control to database.
I tried this following code but its not working please help me.

                  byte[] imagebyt = new byte[Image1.ImageUrl.Length + 1];
MemoryStream ms = new MemoryStream(imagebyt,0,imagebyt.Length);
cmd = new MySqlCommand("Insert Into db_image (Image) values (@imag)", con);
                    cmd.Parameters.AddWithValue("imag", imagebyt);

                    cmd.ExecuteNonQuery();
Posted
Comments
I.explore.code 16-Oct-12 7:30am    
if you try debugging the code, you will find the flaw in your code. Did you? Besides why do you think you would be able to get the bytes from the Image control in ASP.NET? If you are trying to upload an image, don't you think the FileUpload control would be the right way to do it?

1 solution

Try this

C#
byte[] imagebyt = new byte[Image1.ImageUrl.FileBytes.Length+1];
                imagebyt = Image1.Imageurl.FileBytes;
MemoryStream ms = new MemoryStream(imagebyt,0,imagebyt.Length);
cmd = new MySqlCommand("Insert Into db_image (Image) values (@imag)", con);
                        cmd.Parameters.AddWithValue("imag", ms.GetBuffer());
                        
                        cmd.ExecuteNonQuery();


Your problem was that you set the length of the byte array but didnt actually tell them to put your image bytes there
 
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