+ Reply to Thread
Results 1 to 2 of 2

Creating a .bmp file with Put statement

Hybrid View

  1. #1
    Registered User
    Join Date
    03-16-2004
    Location
    UK
    MS-Off Ver
    2003
    Posts
    85

    Creating a .bmp file with Put statement

    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
    Attached Files Attached Files
    Phil

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Creating a .bmp file with Put statement

    The attachment may help for debugging purposes -- it reads bmp headers, and will plot the picture and the RGB values (within the limits of Excel 2003's 256 columns) for uncompressed bmps with 24-bit color.

    Please don't look closely at the code -- it's oooold.
    Attached Images Attached Images
    Attached Files Attached Files
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1