Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1471
  • Last Modified:

GetDIBits in Borland C++ Builder

Hi:
 (1).
   I would like to get the pixels color of TImage in BCB.
   So, I found all win32 and method of TImage,
   it was the only method GetDIBits.
   Part of the program is:

   // begin
   HDC hDC = Image->Canvas->Handle;
   HBITMAP hBitmap = CreateCompatibleBitmap(hDC,400,400);
   int kk= GetDIBits(hDC,
                     hBitmap,
                     0,
                     400,
                     lpBuf,
                     (LPBITMAPINFO) &BMI,
                     DIB_RGB_COLORS);
     Edit1->Text = IntToStr(kk);
     // end
     But the above kk is zero,the method can't work.
     Hope any help.
 (2).
     The another method is that usimg for loop we move
     the Image->Canvas->Pixels to lpBuf.
     Have any other method?
0
mathematics
Asked:
mathematics
  • 3
  • 2
1 Solution
 
msmitsCommented:
There are a couple of point that you may have to correct:

1. you have to fill the biSize field of the BITMAPINFOHEADER structure:

  BMI.bmiHeader.biSize = sizeof(BITMAPHEADERINFO);

2. before you call GetDIBits with your lpBuf, you need to fill in the first 6 fields of the same BITMAPHEADERINFO structure. After doing step 1, it is easiest to call GetDOBits twice (the first time with NULL to get the bitmap info and the second time to get the bits):

   int kk = GetDIBits(hDC, hBitmap, 0, 400, NULL,
     (LPBITMAPINFO) &BMI, DIB_RGB_COLORS);
   kk = GetDIBits(hDC, hBitmap, 0, 400, lpBuf,
     (LPBITMAPINFO) &BMI, DIB_RGB_COLORS);

One of the fields of the BITMAPINFOHEADER is also the size of the bitmap image. You can use this to allocate enough memory for the second call (so do the new or malloc in between).

Your second methods sounds a bit time consuming. For every pixel you have to go to the API.

0
 
mathematicsAuthor Commented:
Is the following statement right?

//
HDC hDC = Image->Canvas->Handle;
HBITMAP hBitmap = CreateCompatibleBitmap(hDC,400,400);
//

I can't get the correct result. Please give me some suggestions.
In the MS help,GetDIBits topic has the remark:

The bitmap identified by the hbmp parameter must not be selected into a device context when the application calls this function.
0
 
msmitsCommented:
That is almost the exact code that I used. I don't have my code with me at the moment, but in about 6 hours I can post the complete code of my Button3Click() code which gets the bits info into a local array. This code was based on your original code and extended with the necessary declarations.
My image was called Image1, so I used Image1 instead of Image, but you may have changed the name of the TImage control.

0
 
msmitsCommented:
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    unsigned char Buffer[200000];
    BITMAPINFO BMI;
    char msg[256];

    BMI.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    HDC hDC = Image1->Canvas->Handle;
    HBITMAP hBitmap = CreateCompatibleBitmap(hDC, 400, 400);
    int kk = GetDIBits(hDC, hBitmap,
                                  0, 400, NULL,
                        (LPBITMAPINFO) &BMI,
                        DIB_RGB_COLORS);
    kk = GetDIBits(hDC, hBitmap,
                                  0, 400, Buffer,
                        (LPBITMAPINFO) &BMI,
                        DIB_RGB_COLORS);
    sprintf(msg, "%d lines.", kk);
    ::MessageBox(NULL, msg, "Info", MB_OK);
}

0
 
mathematicsAuthor Commented:
Thanks,msmits.
I try the following codes to test if the function
GetDIBits work well:

// connect the above code of yours
  StretchDIBits(Image2->Canvas->Handle,0,0,
                BMI.bmiHeader.biWidth,BMI.bmiHeader.biHeight,
                0,0,
                BMI.bmiHeader.biWidth,BMI.bmiHeader.biHeight,
                Buffer,(LPBITMAPINFO) &BMI,
                DIB_RGB_COLORS,SRCCOPY);
// end

(1) You can find something. bugs of BCB or vcl is not     compatible with win32. Hope I am wrong.
(2) I try StretchDIBits work well if Buffer is read from
    FILE.
(3) I try three days about this question.
(4) Thanks. If you can tell me the sentence in Help of MS Help
    about GetDIBits:
 
The bitmap identified by the hbmp parameter must not be selected into a device context when the application calls this function.

The question is:
  Is the hBitmap selected by Image->Canvas->Handle?

I think so.

(5) Come back the original question:
    How do we retrieve the bits of the specified TImage
    and copies them into a buffer using the specified format?
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now