Need Help Creating and Displaying a DIB

Need Help Creating and Displaying a DIB

Post by John O'Ne » Wed, 08 Aug 2001 00:10:41

I am trying to represent a gradient as a bitmap and diplay it on a
form.  I have RGB values that I read in from a file, which I store in
the BITMAPINFO structure's color table.  After I create the DIB in the
Form's initialization method, I try to display it when an OnPaint
event occurs.  I am not sure if the RGB values should be stored in the
color table or in the pixel data though.  The bitmap is only drawn as
a solid color when I run the program.  Here is the code in which I try
to create and display the gradient:


        TImage *MetricBar;
        BITMAPINFO *bmpinfo;
        HBITMAP bmp;


void __fastcall TForm1::FormCreate(TObject *Sender)
  // Read in the RGBQUAD array from the file

    int junk, r, g, b;
    int i, j;
    int BitmapSize; // Size of the bitmap

    // Set the dimensions of the bitmap
    const int bmpHeight = 256;
    const int bmpWidth = 30;
    // Load the RGB array from the file
    std::ifstream bitmapFile("../colormaps/Jet.dat",

    // header size + color table size + pixel size
    BitmapSize = sizeof(BITMAPINFOHEADER) + (sizeof(RGBQUAD) *

    bmpinfo = (BITMAPINFO *) malloc(BitmapSize);

    bmpinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmpinfo->bmiHeader.biSize = BitmapSize;
    bmpinfo->bmiHeader.biWidth = bmpWidth;
    bmpinfo->bmiHeader.biHeight = bmpHeight;
    bmpinfo->bmiHeader.biPlanes = 1;
    bmpinfo->bmiHeader.biBitCount = 8;
    bmpinfo->bmiHeader.biCompression = BI_RGB;
    bmpinfo->bmiHeader.biSizeImage = 0;

    // Set the color table
    for (i = 0 ; i < 256; i++)
       bitmapFile >> junk;
       bitmapFile >> r;
       bitmapFile >> g;
       bitmapFile >> b;
       bmpinfo->bmiColors[i].rgbRed = (BYTE)r;
       bmpinfo->bmiColors[i].rgbGreen = (BYTE)g;
       bmpinfo->bmiColors[i].rgbBlue = (BYTE)b;
       bmpinfo->bmiColors[i].rgbReserved = (BYTE)0;


    // Close the file, now that we are done with it

    int index;
    BYTE *Pixels = new BYTE[bmpWidth * bmpHeight];

    // Set the pixels of the bitmap
    for(i=0; i<bmpHeight; i++)
       for(j=0; j<bmpWidth; j++)
          index = i*bmpWidth + j;
          *Pixels++ = i;

    // Create a DIBSection
    HBITMAP HBitmap = CreateDIBSection(NULL, bmpinfo, DIB_RGB_COLORS,
                                       (void **)&Pixels, NULL, 0);

    // Set the class bitmap pointer to the DIB that was just created
    bmp = HBitmap;


void __fastcall TMetricDialog::FormPaint(TObject *Sender)

    if (bmp)

        // create a device context
        HDC Hdc = CreateCompatibleDC(MetricBar->Canvas->Handle);

        // select the DIBSection into the DC
        HBITMAP HOldBitmap = SelectObject(Hdc, bmp);

        // display the changes on the form
        BitBlt(MetricBar->Canvas->Handle, 0,
               bmpinfo->bmiHeader.biHeight, Hdc, 0, 0,SRCCOPY);


        // clean up
        SelectObject(Hdc, HOldBitmap);
    else ShowMessage("failed to create DIBSection");


            Thanks in advance for any help,

1. Need help with DIB

Hi :o)

I have a problem with Delphi 1.0 and bmp's.
What i have is a program which load a bmp file, and the copies it to the

The problem is, i need this image to be device independant, or else the
application which is to recive the bitmap uses ages to paste it. So my
question is this. How can i tell Delphi to Copy the bmp to the clipboard as

Today i use

If i first paste the picture in word, and then copy it again.... its DIB on
the clipboard... How can i do from Delphi what word does ???

Im pretty new to Delphi programming, and would appreciate ANY help....

Cheers  :o)
Bjorn Isaksen

2. Shadowing Alogrithms

3. Dib Displaying in Windows

4. (no subject given!)

5. Strange results with display profile created with Eye One Display

6. HELP!!! - What to do about high traffic?

7. creating a display list/loading a texture after 1st display in gl4java

8. DSL Availability In Your Area? .................... IWOlRBxu

9. DIB to DX8 Display

10. Displaying a DIB

11. Dll returns a Handle to a DIB how can I display in picture box

12. Displaying DIB's

13. displaying 24 bits DIB bitmap