Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 688
  • Last Modified:

Convert an Image/Bitmap Object to Byte Array without specifying a format?

How can I convert an Image or Bitmap object to a byte array without specifying an image format?  I want to take a bitmap object and save it to sql server preserving its existing format.  Every sample I've seen includes specifying the format when you call Image.Save, and I dont want it changing the fileformat on me.

I've tried routines that use the ImageConverter object and have also try saving to a MemoryStreak and then reading it out to a byte array.   So far all methods I've tried result in a "General GDI+ Error".

Here's my latest code that throws an error on line 2.

            Dim ms As New MemoryStream
            Me.Image.Save(ms, Imaging.ImageFormat.Bmp)
            Dim buffer(ms.Length - 1) As Byte
            ms.Seek(0, SeekOrigin.Begin)
            ms.Read(buffer, 0, ms.Length)
            Return buffer

However, I've noticed in debug mode, after the error is thrown, I can reinitialzie my memorystream, run it again, and it works fine.  So it only throws the error every other time.
0
gregspence
Asked:
gregspence
  • 2
  • 2
1 Solution
 
Erick37Commented:
When you say you do not want to *change* the fileformat, does this imply that you want to save the image in the same format as the file was originally loaded from disk (jpg, png, bmp, etc) ?
0
 
gregspenceAuthor Commented:
Yes thats correct.  I'm loading both TIFF and greyscale JPEG images, and I want to preserve that format when they are saved in the database.  So all the examples that show specifying a certain format dont work for me.  I also tried passing in the "RawFormat" of the image object and it still throws an error.  It seems no matter what I do I get a generic GDI+ error.
0
 
Erick37Commented:
If you are not modifying the image (resizing, adding text, etc) then you can read the original file from disk directly into a byte array.

If you are manipulating the original image, then you will have to read the file headers and extract the information on how to save the picture back to disk in the original format. (Assuming the original format is supported in the Image class)

Please correct me if I'm wrong about this, experts, but I believe that once you have loaded a picture into an Image object, you lose all the format specific information.
0
 
gregspenceAuthor Commented:
Unfortunately I will need to be able to crop the image.  I did more research yesterday and found that my General GDI errors were occurring most likely due to the format of the image.  The image was a 256 greyscale 8bpp JPEG.  Everytime I tried to save the image or do anything with it, an error was thrown.

I solved the problem by creating a new bitmap using the 16bpprgb format and using the graphics object to "DrawImage" from the source bitmap to the new bitmap.  At that point, I could save the new bitmap just fine without receiving any errors.  This allowed me to save it to a memory stream and then read the byte array from the stream.

Thanks for the responses, I am going to close the question.
0
 
ee_ai_constructCommented:
Question answered by asker or dialog deemed valuable.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now