Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a Bitmap 1

Status
Not open for further replies.

SDE1

Programmer
Mar 11, 2001
7
0
0
GB
I want to create a bitmap that is based from data i have created from various processes that is stored in an array.

I need to write the bitmap FileHeader and bitmap InfoHeader to a file then append the data from the array into the file.

Please can someone show me how to do this or better still, provide code that will enable me to do this. Most code i have seen involves using API calls to fill a picture box then change its size etc. I do not want to do this, i just want to save a bmp file to disk based on data i have stored in an array.

Thankyou

Simon
 
Basically what you need to know is the file format of a .bmp file. There is no quick and dirty way that I know of where you can simple say SaveBitmap other that the SavePicture method of vb. (of course that means you would have to load the data to the control and then save it, which is not what you want to do.

So if you want to do it straight from code with no API calls and no picture or image controls, then you have to learn the file format. Check out www.wotsit.org and you can study the format of a .bmp file.
 
In principle (assuming a bitmap with no palette), it is as easy as (the following is NOT a working program):
[tt]
Private Type BITMAPFILEHEADER
bfType As Integer ' set to 19778
bfSize As Long ' Entire file size
bfReserved1 As Integer ' 0
bfReserved2 As Integer '0
bfOffBits As Long ' byte offset from the beginning of the file at which the bitmap data starts
End Type

Private Type BITMAPINFOHEADER
biSize As Long ' size of BITMAPINFOHEADER structure
biWidth As Long ' Width of bitmap in pixels
biHeight As Long ' Height of bitmap in pixels
biPlanes As Integer ' No of planes. Can normally be set to 1
biBitCount As Integer ' Bitmap bit depth. Set to 24 if you want to avoid setting up a palette, and reading an RGBQuad structure
biCompression As Long ' Set to 0
biSizeImage As Long ' Size of bitmap image data in pixels. Not necessarily equal to width*height
biXPelsPerMeter As Long ' set to 0
biYPelsPerMeter As Long ' set to 0
biClrUsed As Long ' set to 0
biClrImportant As Long 'set to 0
End Type

Dim BMPFileHeader As BITMAPFILEHEADER
Dim BMPInfoHeader As BITMAPINFOHEADER
Dim BMPData() As Byte ' This represents your array of data

' Open a file here in binary mode
Put filehandle, 1, BMPFileHeader
Put filehandle, , BMPInfoHeader
Put filehandle, , BMPData
[/tt]
OK, that's the theory. It get's a little tricky though, because each scanline actually needs to be (zero) padded to a 32 bit boundary. Eg, if your bitmap was just 1 pixel wide in 24-bit mode, you actually need to add eight 0 bits to actually legally describe that scan line - so you would need to start mucking about with your array. And the biSizeImage member of the BITMAPINFOHEADER needs to take this padding into account.

Have fun...
 
Heres some help to deal with 4 byte boundary.
( BmpH=BmpFileheader and BmpI=bmpinfoheader )

Linelength=(BmpH.bfsize - BmpH.bfoffsetbits)/bmpI.biheight

This gives line lenght in bytes. For 8 and 24 bit pixels,
use the bytesperpixel field to calculate the actual linelength.

Reallength=bytesperpixel*imagewidth/8

Now the throw way padding is;
throwAwaypadding=linelength-Reallength

Also, the bmp is stored bottom line first. for example, an image like this (0 and 1 represent a pixel each)

0000100
0001100
0011100
1111111
0111110

would be stored like this

0111110
1111111
0011100
0001100
0000100

In the 24bitpixel mode colors are stored in the order blue green red so each pixel can be defined as

Type pix
Bl as byte
Gr as byte
Re as byte
end type

In 8 bit mode, you need to define a 256 color lookup palette. this goes in right after the info header. it starts at
PaletStart=lenB(bmpH)+lenb(Bmpi)


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top