?
Solved

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

Posted on 2005-04-05
6
Medium Priority
?
680 Views
Last Modified: 2008-02-01
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
Comment
Question by:gregspence
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 13710338
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
 
LVL 1

Author Comment

by:gregspence
ID: 13710522
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
 
LVL 32

Expert Comment

by:Erick37
ID: 13713373
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
 
LVL 1

Author Comment

by:gregspence
ID: 13717024
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
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 13747366
Question answered by asker or dialog deemed valuable.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question