Re: bitmap
From: Mike D Sutton (EDais_at_mvps.org)
Date: 11/19/04
- Next message: Mike D Sutton: "Re: Show embedded image from binaryfile"
- Previous message: christophe-pasde<>: "Re: bitmap"
- In reply to: Teja: "bitmap"
- Next in thread: Teja Nanduri: "Re: bitmap"
- Reply: Teja Nanduri: "Re: bitmap"
- Messages sorted by: [ date ] [ thread ]
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/
- Next message: Mike D Sutton: "Re: Show embedded image from binaryfile"
- Previous message: christophe-pasde<>: "Re: bitmap"
- In reply to: Teja: "bitmap"
- Next in thread: Teja Nanduri: "Re: bitmap"
- Reply: Teja Nanduri: "Re: bitmap"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|