Solved

GetDIBits in Borland C++ Builder

Posted on 1997-09-10
5
1,391 Views
Last Modified: 2008-03-17
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
Comment
Question by:mathematics
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
msmits earned 40 total points
ID: 1169391
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
 

Author Comment

by:mathematics
ID: 1169392
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
 
LVL 3

Expert Comment

by:msmits
ID: 1169393
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
 
LVL 3

Expert Comment

by:msmits
ID: 1169394
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
 

Author Comment

by:mathematics
ID: 1169395
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 76
FMX enumerated colours 2 101
thread-safe code in c++ 2 104
computer science syllabus 3 83
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question