locked
Trouble saving Bitmap object as byte[] RRS feed

  • Question

  • User-1162827054 posted
    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 WWW are 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!
    Tuesday, July 27, 2004 9:44 PM

All replies

  • User-1162827054 posted
    I'm hoping that this construct will work: // convert the image into a byte array Image img = Graphics.FromImage(bmp); Image img2 = null; MemoryStream mem = new MemoryStream(); img2.Save(mem,ImageFormat.Jpeg); byte[] bits = mem.ToArray(); // add SqlParameter comm.Parameters.Add(new SqlParameter("@ImageData",SqlDbType.Image)).Value = bits; There's a helpful warning on why not to opt to save the byte[] to the same stream used to create the image: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDrawingImageClassSaveTopic1.asp
    Tuesday, July 27, 2004 11:37 PM
  • User-1897028626 posted
    I am not sure if this applies to you, but I created an images utility helper class myself although I personally chose to keep track of files added, updated and deleted from a system folder instead of saving them to the database. "But" this may be relevant: I had to change code when my app was deployed to a web host ( i.e. it worked fine on local host ) to be sure that information relating to the image file is translated or converted "before" sending it to the business tier in a situation where there may be a postback event to the webform. Let me explain. Originally, the helper class had several public methods and I would make calls to the images utility class methods in sequence, multiple calls between the webform and the utility class just did not work. What did work is to create a single public method to send the object definition of the http posted file to that image utility class that then calls a series of private methods instead. So instead of 1. Checking to see if the image file format is valid, and then 2. Uploading the image, I send the http posted file straight to the helper class that performs all interpretation and interpolation of image files in one fell swoop and returns error messages if the file is too large or if the file is an incorrect image file format, for example. Will your code work if you add the image file object to a byte array at the page level and then send it to your business tier?
    Friday, August 27, 2004 2:35 PM