Converting a bitmap to byte array

Funger

Member
Joined
Dec 15, 2006
Messages
8
Programming Experience
5-10
Hi all!

I have a problem that has had me confused for a while. I need to be able to convert an array of bytes into a bitmap, and vice versa.

I found this bit of code that takes a bitmap and converts it to a byte array.

VB.NET:
' Load bitmap From File
Dim file_name As String = "C:\temp.bmp"
Dim bm As New Bitmap(file_name)

'Convert loaded bitmap into a byte array
Dim ms As MemoryStream = New MemoryStream
Dim ba As Byte()
bm.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
ba = ms.ToArray()
ms.close()
The only problem is that it appends header data to the beginning of the array, and I am only interested in the pixel data. My bitmaps/byte arrays are all greyscale images with 0 being black, and 255 being white and set width and heights.

I need to know how to convert a bitmap into a byte array WITHOUT the header at the beginning. Then, I also need to know how to create the header and append it to the array when I need to convert from a byte array back into a bitmap.

Can anyone please help me? Thanks in advance!

-Funger
 
It appears that you're trying to load the entire file, and not just a mapping of bits, into the byte array. What you could try to do is to "skip" the first 53 or so bytes, which is the approximate length of the header, and just store the bitmap (see: Windows bitmap format).

I'm assuming there's a fixed width and height stored in the program.
 
That could be done if you followed the bitmap file format to the full, not just cutting after header but you'd needed to get the structures and exact info about where the bitmap data started from them also.

Fortunately the .Net Bitmap class has got unlocking features to get to the unmanaged byte data, namely the LockBits/UnlockBits methods. Here I've taken a bitmap (from my.resources) and grabbed the vital info:
VB.NET:
Dim bmp As Bitmap = My.Resources.[SIZE=2]_1126972183_33714[/SIZE]
Dim [B][COLOR=darkgreen]pf[/COLOR][/B] As Drawing.Imaging.PixelFormat = bmp.PixelFormat
Dim [B][COLOR=darkgreen]sz[/COLOR][/B] As Size = bmp.Size
Dim rct As New Rectangle(New Point(0, 0), sz)
Dim bmd As Imaging.BitmapData = bmp.LockBits(rct, Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat)
Dim [B][COLOR=darkgreen]stride[/COLOR][/B] As Integer = bmd.Stride
Dim totalbytes As Integer = stride * sz.Height
Dim [COLOR=darkgreen][B]bytes[/B][/COLOR](totalbytes - 1) As Byte
System.Runtime.InteropServices.Marshal.Copy(bmd.Scan0, bytes, 0, totalbytes)
bmp.UnlockBits(bmd)
bmp.Dispose()
The bytes array now contains only the byte data of bitmap. Key info kept needed to recreate the bitmap from bytes is Size, Stride and PixelFormat. (Stride may be calculated from pixelformat and image width also.)

Here is creating a new bitmap from the key data (I've reused some variables from the extraction example):
VB.NET:
Dim bmp2 As New Bitmap(sz.Width, sz.Height, pf)
bmd = bmp2.LockBits(rct, Imaging.ImageLockMode.WriteOnly, pf)
System.Runtime.InteropServices.Marshal.Copy(bytes, 0, bmd.Scan0, bytes.Length)
bmp2.UnlockBits(bmd)
 
[SIZE=2]'look at the result[/SIZE]
[SIZE=2]PictureBox1.Image = bmp2[/SIZE]
 
Back
Top