Groups | Blog | Home
all groups > dotnet drawing api > july 2003 >

dotnet drawing api : HEEEELP: Getting a BITMAPINFOHEADER ptr out of .NET Bitmap class - HOW?


Ken Varn
7/3/2003 10:26:42 AM
I have had to do something similar to what you are describing. I had to
load a BITMAPINFOHEADER data pointer into a Bitmap object. The way I did it
was to use a MemoryStream in the Bitmap constructor. I am assuming that you
could do the reverse using the Bitmap.Save method. One thing that I found
out on loading an image is that the Bitmap object expects a BITMAPFILEHEADER
to precede the BITMAPINFOHEADER when loading the bitmap.

Here is my suggestion for extracting the BITMAPINFOHEADER, but I can't say
for sure if this would work because I have not tried it.

1. Create a MemoryStream object.
2. Call the Bitmap.Save() method passing the MemoryStream object from step
1. You may need to specify the BMP coded for the save.
3. Read the MemoryStream contents into a byte array (MemoryStream.Read or
MemoryStream.ToArray).
4. Pin the first element of the byte array to a C++ memory pointer.
5. Parse the memory pointer for the BITMAPINFOHEADER. Note that a
BITMAPFILEHEADER will probably be at the start of the bitmap memory pointer.
You probably need to offset past that header.
6. Send the BITMAPINFOHEADER pointer to your function that requires it.


--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
varnk@diebold.com
-----------------------------------
[quoted text, click to view]

José Joye
7/3/2003 2:52:35 PM
Hello,

In my Managed C++ code, I have an instance of the .NET Bitmap class and I
need to call a non managed routine within a third party library which expect
a a pointer to BITMAPINFOHEADER

Is there a simple way to get this information out of the .NET Bitmap class?

I know of the:
IntPtr hBitmap = BM->GetHbitmap();

However, it sounds like a nightmare to go from this point on to my target.

Does anyone has a working sample. I googled for hours and I still do not see
how to proceed.

Thanks,
José

José Joye
7/3/2003 4:35:48 PM
Hi,

In fact, Idid have already the stream in memory. But I did not know what to
do with it. Therefore, I tried to push it in the CTOR of the Bitmap ... not
too clever... q:-)

I followed your explanation up to : "You probably need to offset past that
header"
What do you mean by that?

José

[quoted text, click to view]

Ken Varn
7/7/2003 2:54:29 PM
Generally, Windows bitmap files contain a BITMAPFILEHEADER that is the first
record. Immediately after this header is the BITMAPINFOHEADER. So, if you
save the image in windows bitmap format, the image data looks something like
this:

BITMAPFILEHEADER
BITMAPINFOHEADER
Raw Bitmap Data

Check your MSDN standard Windows GDI section for a description of these two
headers. Note that Microsoft also has a structure defined called
BITMAPINFO. This structure is laid out as follows:

struct BITMAPINFO
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
};

This structure is interesting in that it allows you to point it to a bitmap
block to get the header that follows the BITMAPFILEHEADER, and the image
bytes that follow. The RGBQUAD section is variable length and can contain
as many records as is indicated by the bmiHeader.

You would think there would be one structure to handle both BITMAPFILEHADER
and BITMAPINFOHEADER, but since there are different versions of the lengths
of these structures, there is no way to utilize a structure to accommodate
them.

There are so many variations on how BITMAPINFO is used, that it would take
awhile to explain the ones that I know in an e-mail. Some elements point to
offsets that get you to other sections of the data. Look in MSDN or search
on the web. There is a lot of information on how these data blocks are used
for various image formats.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
varnk@diebold.com
-----------------------------------
[quoted text, click to view]

AddThis Social Bookmark Button