Re: bitmap

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Mike D Sutton (EDais_at_mvps.org)
Date: 11/19/04


Date: Fri, 19 Nov 2004 11:22:23 -0000


> I have a bitmap file loaded into memory as an array of
> bytes( the bitmap header and everything that is in a bmp
> file). Is there any way of getting the rgb value of any
> pixel in the bitmap. Or is it possible to atleast load this
> in to a picturebox.

To convert this to a StdPicture object that can be displayed in a picture box etc have a look at the "graphicload.zip" example here
and see if it's of any use to you:
http://www.mvps.org/btmtz/_misc/index.htm
If you want to grab the data from the Bitmap without having to convert the entire thing to a StdPicture you need to extract the
Bitmap headers from the data stream to get the offset to the data and format of colour values used by the file. If you have a full
.BMP file in memory then the data will be prefixed with a 14-byte BITMAPFILEHEADER structure (you can test for the presence of this
structure by comparing the first two bytes with ASCII "BM" which is the Bitmap file signature.)

'***
Private Type BitmapFileHeader ' 14 bytes
    bfType As Integer
    bfSize As Long
    bfReserved1 As Integer
    bfReserved2 As Integer
    bfOffBits As Long
End Type
'***

The important part of this structure is the bfOffBits member which defines the offset to the start of the image data from the
beginning of the stream, however unfortunately this member isn't always filled and if your buffer contains a packed DIB then this
structure won't be present at all so I'll explain how to work this out in a second.
After the file header comes the info header, however at this point I'll direct you to one of my prior posts on the subject which
explains the process in more detail and presents some example code:
http://groups.google.co.uk/groups?selm=utxksBNwEHA.4048%40TK2MSFTNGP15.phx.gbl
Don't be put off by all the references to pointers and packed DIB's in the previous post, the principle and data format is exactly
the same as what you're doing.
Once you have the BitmapInfoHeader and offset to the start of the data then you need one final piece of information before you can
grab individual pixels from the data and that's the scanline width which you can work out from this method:

'***
DIBScanline = (((Width * BitDepth) + &H1F) And Not &H1F) \ &H8
'***

You can now grab a pixel from the data stream by working out it's offset from the start of the data, however in most cases the DIB
scanlines are stored in bottom to top ordering so you need to take that into account when working out the offset.

'***
YOff = ((Height - 1) - Y) * DIBScanline
XOff = X * (BitDepth / 8)

PixOffset = DataStart + YOff + XOff
'***

Where X and Y define the coordinates of the pixel you're after, you can then read (BitCount / 8) bytes from this offset which will
contain the pixel value.
Note; if the info header defines a negative height member then you don't need to flip the Y axis and you must use the Abs() function
wherever you use the height member of the header.
If you're working with a paletted image (BitDepth <= 8) then this maps to an entry in the palette which immediately follows the info
header. For 1 and 4-bit images you will also need to extract the desired _bits_ from the data stream before mapping them to the
palette, however unless you actually need to deal with this situation then I won't confuse you further with the details on that!

So in summery the steps you need are:
1) Find out whether you have a file header and get it's data offset
2) Grab the info header
3) Either using the value from (1) or by working it out from (2) find the data offset
4) Get the DIB scanline width
5) Work out the offsets to the desired pixel and read data

Hope this helps,

    Mike

 - Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: http://EDais.mvps.org/



Relevant Pages

  • Re: Newbie: Bamboozled
    ... But when I write the bitmap headers as understood from the ... specifications I found when googling specifically the offset ... I used od -d to look at the header that I was producing and the header ... the expected 14 bytes by not writing the last two bytes to the file. ...
    (comp.unix.programmer)
  • Re: problem with AVI getframe modify pixels and display
    ... >> The part I am having trouble with is to modify the bitmap contents. ... >> guess I am having trouble converting the packed DIB to a DIBSection ... > To work out the size of the DIB header, ... > The next step is to work out the palette size, ...
    (microsoft.public.vb.winapi.graphics)
  • Re: problem with AVI getframe modify pixels and display
    ... > display each frame to a picture box. ... > The part I am having trouble with is to modify the bitmap contents. ... To work out the size of the DIB header, ... The next step is to work out the palette size, generally the palette is only included in <=8-bit images, however based on the Bitmap ...
    (microsoft.public.vb.winapi.graphics)
  • Re: Hiding Data in jpg or bmp files
    ... In the case of a bitmap it's very easy to store extra data right after the BITMAPINFO header block simply by setting the correct ... offset to the start of the data in the BITMAPFILEHEADER structure. ... This vulnerability was discovered due to the leaking of some parts of the Windows 2000 source code. ...
    (microsoft.public.vb.general.discussion)
  • Re: 555 or 565
    ... Unfortunately GetDIBitsretrieves the bitmap in display-compatible form, ... handling functions do implicit format conversion to comply with the current ... > BITMAPINFOHEADER structure will get you the header, ...
    (microsoft.public.win32.programmer.gdi)