Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

GetDIBits in Borland C++ Builder

Posted on 1997-09-10
5
1,399 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall‚Ķ
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

829 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