I am trying to craete a bmp file programatically. (It is for a GIS style application in excel where I can make full use of the colour pallate and use a very large grid. I've explored charting options and shape objects but they have limitation. This will be a much better solution).
Here some of my test code to create a bmp. I cribbed it from an example and changed some of the header parameters and the pixels to try and craete a picture with random dots. It kind of works in that it doesnt crash and creates a file with all the correct bytes in as far as I can tell (I read them back in and compare them with another bmp).
When I actually open the file it is just black with dimensions 1392x1040 pixels. Not what I've specified.
Any suggestion on what I'm getting wrong?
Here's the code.
Sub BMP_Gen()
Sheets("Sheet1").Select
Dim bmpHeader As bmp8
With bmpHeader.bmiHeader
.biSize = Len(bmpHeader.bmiHeader)
.biWidth = 1392 ' pixel width of bitmap
.biHeight = 1040 ' pixel height of bitmap
.biPlanes = 1
.biBitCount = 8 ' 8 bits per pixel
.biCompression = 0 ' not compressed
.biSizeImage = .biWidth * .biHeight
.biXPelsPerMeter = Int(72& * 1000 / 25.4)
.biYPelsPerMeter = .biXPelsPerMeter
.biClrUsed = 0 ' all colours may be used
.biClrImportant = 0 ' all colours are important
End With
With bmpHeader.fileHeader
.bfType = &H4D42
End With
Dim m As Long, h As Long, v As Long, vIn As Long
Open "c:\tmp5.bmp" For Binary As 1
vIn = Cells(1, 6)
Put 1, , vIn
For m = 2 To 68
h = Cells(m, 5)
v = Cells(m, 6)
Put 1, h + 1, v
Next m
Close 1
Dim b1() As Byte
Open "c:\tmp5.bmp" For Binary As 1
ReDim b1(0 To LOF(1) - 1)
Get 1, 1, b1()
Close 1
Dim n As Long
Dim Txt As String
For n = 0 To UBound(b1)
UserForm1.List1.AddItem Format(n) & vbTab & Format(b1(n))
Txt = Txt & Format(n) & vbTab & Format(b1(n)) & vbLf
Cells(n + 1, 8) = Format(n)
Cells(n + 1, 9) = Format(b1(n))
Next n
End Sub
Bookmarks