Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GetDIBits in Borland C++ Builder

Posted on 1997-09-10
5
Medium Priority
?
1,454 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
[X]
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
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
msmits earned 80 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

721 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