Converting a byte array to a bmp

G

Guest

Hello all,

I have a byte array that I would like to try and get converted to a bmp image.

I have tried to use this code to do it:

====================================================

Dim imgBuffer(614400) As Byte
Dim ms As MemoryStream = New MemoryStream(imgBuffer)
Dim bmp As Bitmap = New Bitmap(ms)

----------------------------------------------
The dll routine called is:
----------------------------------------------

Private Declare Function hpCamPreviewGetBGR16 Lib "hpcamapi.dll" ( _
ByRef hCamera As IntPtr, _
ByVal pBuffer As Byte()) As UInt32

===================================================

This does not seem to do the trick.

Does anyone have any ideas on how to achieve this in VB.NetCF?

The returned frame in the buffer is suppose to be (width x height x 2) bytes
in size and each pixel is 1 word (2 bytes) in size, with the following format:

RRRRRGGGGGGBBBBB
5-bit LSB is for blue pixel
5-bit MSB is for red pixel
 
A

Alex Feinman [MVP]

Won't work. MemoryStream-based consrtuctor takes the same bystream found in
an image file, i.e. with header and stuff. YOu will need to build missing
parts in memory yourself. The absolutely easiest thing to do is to take a
bmp file of the same dimensions and color depth as your image, and using hex
editor (Visual Studio allows you to open a file as binary) strip everything
past the header. The header in bitmaps created by MSPaint is exactly 0x36
bytes. Delete everything past 0x36 and save to a new file. Include this file
in your project as content. In your code simply open a stream on this file
and read it into a memory stream, then append the data returned from HP
camera and pass th resulting stream to the Bitmap() constructor
 
A

Alex Yakhnin

Another possible solution. Take a look at my post: How to create the managed
Bitmap from native HBITMAP handle.

(http://blog.opennetcf.org/ayakhnin/PermaLink.aspx?guid=533715eb-898c-4f60-8560-e1ae9bed9cc3)

It includes the BitmapBuffer class. Create an instance of this class with
appropriate values for width, height and bitCount and copy the bitmap byte
array you get from the camera to the buffer member variable...

--
Alex Yakhnin, .NET CF MVP



Alex Feinman said:
Won't work. MemoryStream-based consrtuctor takes the same bystream found
in an image file, i.e. with header and stuff. YOu will need to build
missing parts in memory yourself. The absolutely easiest thing to do is to
take a bmp file of the same dimensions and color depth as your image, and
using hex editor (Visual Studio allows you to open a file as binary) strip
everything past the header. The header in bitmaps created by MSPaint is
exactly 0x36 bytes. Delete everything past 0x36 and save to a new file.
Include this file in your project as content. In your code simply open a
stream on this file and read it into a memory stream, then append the data
returned from HP camera and pass th resulting stream to the Bitmap()
constructor
 
C

Christian Neubauer

Alex,

I've tried using your bitmap converting code along with a version by
Alex Feinman and another by Microsoft to save raw image data from my
rx3715 built in ipaq camera to a bitmap. All three different versions
seem to work but all three produce an image with a bluish tinge in the
lighter areas of the picture. I am limited to capturing 16 bit 160x120
images and I adjusted the various bitmap savers to deal with 16 bit
images but I am still getting this problem. Do you have any idea what
could be causing this? I have played with the white balance, color,
and metering and nothing has any effect on the image. If I call a
different function in the camera API to save the image directly to a
JPG file then it works fine and the image looks normal. I would
appreciate any ideas on what could be causing this. Since the JPG
saving works fine, I assume it has something to do with my conversion
to of bytes to bitmap.


Thanks,
Christian


Alex said:
Another possible solution. Take a look at my post: How to create the managed
Bitmap from native HBITMAP handle.

(http://blog.opennetcf.org/ayakhnin/PermaLink.aspx?guid=533715eb-898c-4f60-8560-e1ae9bed9cc3)

It includes the BitmapBuffer class. Create an instance of this class with
appropriate values for width, height and bitCount and copy the bitmap byte
array you get from the camera to the buffer member variable...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top