Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a bitmap header to a byte array then create a bitmap file

Tags:

c#

image

bitmap

I have a ByteArray from a camera,it's just a stream and has no bitmapheader.

I have searched for a demo,but they are all in C++.

I try to add a bitmapheader to my code, but a ParameterException is thrown. How do I make a bitmap file from the ByteArray?

like image 810
ypc Avatar asked Jul 12 '12 12:07

ypc


People also ask

What is a header a bitmap?

DIB header (bitmap information header) This block of bytes tells the application detailed information about the image, which will be used to display the image on the screen. The block also matches the header used internally by Windows and OS/2 and has several different variants.

What is a bitmap header?

The bitmap header is like a recipe for a bitmap image. It includes the essential information about width, height, bit per pixels, length of the data, and etc in order to make a proper array for the image.

Does a ByteArray have a bitmap header?

I have a ByteArray from a camera,it's just a stream and has no bitmapheader. I have searched for a demo,but they are all in C++.

What is a bitmap image?

Technically, a Bitmap image is an uncompressed file format which means, every pixel of an image has its own bit (or group of bits) in the file. However, some other formats like PNG and JPEG, they use some compression methods to group similar pixels to decrease overall file size.

Can I convert a byte[] array to a bitmap in Windows Phone?

I have a byte [] that comes from a WCF service. I can successfully convert the array to a Bitmap in Windows Phone but the Bitmap is null when I use the DecodeByteArray method in an Android project. I'm still having the issue. Images are now all RGB formatted.


1 Answers

You can't make a bitmap class directly with a raw data that doesn't have the bitmap header. The bitmap header is like a recipe for a bitmap image. It includes the essential information about width, height, bit per pixels, length of the data, and etc in order to make a proper array for the image.

Making the bitmap header is fairly easy if you take a close look at the bitmap header structure here


But nothing is better than a complete example of a source code.

I broke them down into some pieces and commented on the each steps for you throughout the whole process. It probably looks a bit excessive and verbose, but it's going to be a good guide for you I guess.


Following is an example of a 2×2 pixel, 24-bit bitmap (Windows DIB header BITMAPINFOHEADER) with pixel format RGB24.

BMP file format - Wikipedia

#region Bitmap Making...

// BmpBufferSize : a pure length of raw bitmap data without the header.
// the 54 value here is the length of bitmap header.
byte[] BitmapBytes = new byte[BmpBufferSize + 54];

#region Bitmap Header
// 0~2 "BM"
BitmapBytes[0] = 0x42;
BitmapBytes[1] = 0x4d;

// 2~6 Size of the BMP file - Bit cound + Header 54
Array.Copy(BitConverter.GetBytes(BmpBufferSize + 54), 0, BitmapBytes, 2, 4);

// 6~8 Application Specific : normally, set zero
Array.Copy(BitConverter.GetBytes(0), 0, BitmapBytes, 6, 2);

// 8~10 Application Specific : normally, set zero
Array.Copy(BitConverter.GetBytes(0), 0, BitmapBytes, 8, 2);

// 10~14 Offset where the pixel array can be found - 24bit bitmap data always starts at 54 offset.
Array.Copy(BitConverter.GetBytes(54), 0, BitmapBytes, 10, 4);
#endregion

#region DIB Header
// 14~18 Number of bytes in the DIB header. 40 bytes constant.
Array.Copy(BitConverter.GetBytes(40), 0, BitmapBytes, 14, 4);
                    
// 18~22 Width of the bitmap.
Array.Copy(BitConverter.GetBytes(image.Width), 0, BitmapBytes, 18, 4);

// 22~26 Height of the bitmap.
Array.Copy(BitConverter.GetBytes(image.Height), 0, BitmapBytes, 22, 4);

// 26~28 Number of color planes being used
Array.Copy(BitConverter.GetBytes(0), 0, BitmapBytes, 26, 2);

// 28~30 Number of bits. If you don't know the pixel format, trying to calculate it with the quality of the video/image source.
if (image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
{                        
    Array.Copy(BitConverter.GetBytes(24), 0, BitmapBytes, 28, 2);
}

// 30~34 BI_RGB no pixel array compression used : most of the time, just set zero if it is raw data.
Array.Copy(BitConverter.GetBytes(0), 0, BitmapBytes, 30, 4);

// 34~38 Size of the raw bitmap data ( including padding )
Array.Copy(BitConverter.GetBytes(BmpBufferSize), 0, BitmapBytes, 34, 4);

// 38~46 Print resolution of the image, 72 DPI x 39.3701 inches per meter yields
if (image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
{
    Array.Copy(BitConverter.GetBytes(0), 0, BitmapBytes, 38, 4);
    Array.Copy(BitConverter.GetBytes(0), 0, BitmapBytes, 42, 4);
}

// 46~50 Number of colors in the palette
Array.Copy(BitConverter.GetBytes(0), 0, BitmapBytes, 46, 4);

// 50~54 means all colors are important
Array.Copy(BitConverter.GetBytes(0), 0, BitmapBytes, 50, 4);

// 54~end : Pixel Data : Finally, time to combine your raw data, BmpBuffer in this code, with a bitmap header you've just created.
Array.Copy(BmpBuffer as Array, 0, BitmapBytes, 54, BmpBufferSize);

#endregion - bitmap header process
#endregion - bitmap making process
like image 119
hina10531 Avatar answered Oct 23 '22 05:10

hina10531