Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

I Want to Upload Image file to the database as binary data using image control.

i want to uplaod a file from Image1.ImageUrl to the Database
Posted
Comments
joshrduncan2012 28-May-13 9:17am    
What's keeping you from doing so? You are free to proceed...

1 solution

Hi,

public static void PerisitImage(string path, IDbConnection connection)
   {
       using (var command = connection.CreateCommand ())
       {
           Image img = Image.FromFile (path);
           MemoryStream tmpStream = new MemoryStream();
           img.Save (tmpStream, ImageFormat.Png); // change to other format
           tmpStream.Seek (0, SeekOrigin.Begin);
           byte[] imgBytes = new byte[MAX_IMG_SIZE];
           tmpStream.Read (imgBytes, 0, MAX_IMG_SIZE);

           command.CommandText = "INSERT INTO images(payload) VALUES (:payload)";
           IDataParameter par = command.CreateParameter();
           par.ParameterName = "payload";
           par.DbType = DbType.Binary;
           par.Value = imgBytes;
           command.Parameters.Add(par);
           command.ExecuteNonQuery ();
       }
   }
 
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