jasonsalas
IS-IT--Management
Hi,
I'm trying to call a helper method that takes as an argument a Bitmap object, which needs to be converted to a byte array for posting into a database field of type IMAGE. Nothing fancy, the logic's just stumped me.
This is in in business tier, so it's not exactly using a file upload form, which 99% of the examples on the based on.
----------------------
private void SaveImageToDatabase(Bitmap bmp)
{
SqlConnection conn = new SqlConnection("conn_string");
SqlCommand comm = new SqlCommand("comm_string",conn);
// convert the image into a byte array
/* THIS IS THE PART I'M STUCK ON */
MemoryStream mem = new MemoryStream();
byte[] imageAsBits = new byte[mem.Length];
mem.Position = 0;
mem.Read(imageAsBits,0,imageAsBits.Length);
// add SqlParams here
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
What's the proper way to save a Bitmap object as a byte[] so that it nay be used later in code? Thanks!
I'm trying to call a helper method that takes as an argument a Bitmap object, which needs to be converted to a byte array for posting into a database field of type IMAGE. Nothing fancy, the logic's just stumped me.
This is in in business tier, so it's not exactly using a file upload form, which 99% of the examples on the based on.
----------------------
private void SaveImageToDatabase(Bitmap bmp)
{
SqlConnection conn = new SqlConnection("conn_string");
SqlCommand comm = new SqlCommand("comm_string",conn);
// convert the image into a byte array
/* THIS IS THE PART I'M STUCK ON */
MemoryStream mem = new MemoryStream();
byte[] imageAsBits = new byte[mem.Length];
mem.Position = 0;
mem.Read(imageAsBits,0,imageAsBits.Length);
// add SqlParams here
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
What's the proper way to save a Bitmap object as a byte[] so that it nay be used later in code? Thanks!