Solved

GetDIBits in Borland C++ Builder

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now