How to create and display a bitmap programatically?

This is a discussion on How to create and display a bitmap programatically? within the Windows Programming forums, part of the Platform Specific Boards category; I'm trying to create a random 50x50 pixel 256 color bitmap programatically. I have studied several instructions about bitmap creation, ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    How to create and display a bitmap programatically?

    I'm trying to create a random 50x50 pixel 256 color bitmap programatically. I have studied several instructions about bitmap creation, but the process is still unclear for me.

    I have tried to do following:

    - filled the bitmapfileinfoheader with correct values
    - created a BYTE[2500] table with some random values
    - used StretchDIBits to show the bitmap on the screen

    As a result I was able to show some kind of bitmap on the screen, but probably not correctly. I don't understand how the color of the one pixel is constructed from the BYTE table that I have created.

    How do I define which 256 colors the bitmap is using?
    Do I have to define the RGBQUAD -structure for the bitmap?

    *s

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,255
    I'm afraid I don't have the proper information to hand, but if you look at the BITMAPINFO structure, you'll see it has a bmiColors member. Under the default definition this only has one member (It can really be any size, but there's no need to define something that won't always be used), but you'll need 256 RGBQUAD entries for your bitmap. To do this, you'll need a pointer to a BITMAPINFO, like so:-
    Code:
    LPBITMAPINFO lpbi;
    
    lpbi = malloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)); // Make space for the header and the palette
    lpbi->bmiColors[0].rgbRed = ...
    ...
    You'll also need to set the biBitCount member of the header to 8 (8 bits per pixel, 256 colours). Not sure how this translates to the file header, sorry

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    6
    You'll also need to set the biBitCount member of the header to 8 (8 bits per pixel, 256 colours). Not sure how this translates to the file header, sorry
    How about displaying a 256 grayscale image on the screen? If I have a byte[2500] table with values, how do I convert it to a 50x50 grayscale bitmap so that I can display it on the screen? Like 0 being black pixel and 255 white etc.

    thanks!

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> so that I can display it on the screen

    After reading your second post, I need to ask, why do you want a bitmap? To draw a grey scale on the screen you could simply call SetPixel() with the appropriate values from your array. SetPixel() is not the fastest way of doing things, but for a simple spec like that would be fine.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    have a look at this function I use to create bitmap headers for a screen capture.


    Code:
    PBITMAPINFO CreateBitmapInfoStruct(HWND hWnd, HBITMAP hBmp)
    { 
        BITMAP bmp; 
        PBITMAPINFO pbmi; 
        WORD    cClrBits; 
    
        // Retrieve the bitmap color format, width, and height. 
        GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp);
    
        // Convert the color format to a count of bits. 
        cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel); 
        if (cClrBits == 1) 
            cClrBits = 1; 
        else if (cClrBits <= 4) 
            cClrBits = 4; 
        else if (cClrBits <= 8) 
            cClrBits = 8; 
        else if (cClrBits <= 16) 
            cClrBits = 16; 
        else if (cClrBits <= 24) 
            cClrBits = 24; 
        else cClrBits = 32; 
    
        // Allocate memory for the BITMAPINFO structure. (This structure 
        // contains a BITMAPINFOHEADER structure and an array of RGBQUAD 
        // data structures.) 
    
         if (cClrBits != 24) 
             pbmi = (PBITMAPINFO) GlobalAlloc(LPTR, sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*(1<< cClrBits)); 
    
         // There is no RGBQUAD array for the 24-bit-per-pixel format. 
    
         else 
             pbmi = (PBITMAPINFO) GlobalAlloc(LPTR, sizeof(BITMAPINFOHEADER)); 
    
        // Initialize the fields in the BITMAPINFO structure. 
    
        pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
        pbmi->bmiHeader.biWidth = bmp.bmWidth; 
        pbmi->bmiHeader.biHeight = bmp.bmHeight; 
        pbmi->bmiHeader.biPlanes = bmp.bmPlanes; 
        pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel; 
        if (cClrBits < 24) 
            pbmi->bmiHeader.biClrUsed = (1<<cClrBits); 
    
        // If the bitmap is not compressed, set the BI_RGB flag. 
        pbmi->bmiHeader.biCompression = BI_RGB; 
    
        // Compute the number of bytes in the array of color 
        // indices and store the result in biSizeImage. 
        // For Windows NT, the width must be DWORD aligned unless 
        // the bitmap is RLE compressed. This example shows this. 
        // For Windows 95/98/Me, the width must be WORD aligned unless the 
        // bitmap is RLE compressed.
        pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8 * pbmi->bmiHeader.biHeight; 
        // Set biClrImportant to 0, indicating that all of the 
        // device colors are important. 
         pbmi->bmiHeader.biClrImportant = 0; 
         return pbmi; 
     }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  2. Simple Ddraw question. Display bitmap on a screen.
    By tegwin in forum Game Programming
    Replies: 0
    Last Post: 05-22-2004, 05:50 PM
  3. Create a Bitmap
    By SuperNewbie in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2004, 08:43 AM
  4. how to create a greyscale bitmap using setpixel(32-bit_value)
    By Kristian25 in forum Windows Programming
    Replies: 1
    Last Post: 03-05-2003, 07:11 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21