Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

GetDIBits, SetDIBits, BitBlt

Posted on 1999-01-12
9
Medium Priority
?
2,536 Views
Last Modified: 2008-02-26
Someone please give me a simple example (or tutorial) of these functions.  I will be using them with TImage.Bitmap.
0
Comment
Question by:scrapdog
[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
  • 5
  • 2
  • 2
9 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1356077
BitBlt() example...

var
  DC : hDC;
  H : HWND;
begin
  H := GetDesktopWindow;
  DC := GetDC(H);
  BitBlt(DC, 0, 0, Image.Bitmap.Width, Image.Bitmap.Height, Image.Bitmap.Canvas.Handle, 0, 0, SRCCOPY);
  ReleaseDC(H, DC);
end;

This would capture the image of the desktop in your TImage component's bitmap...

-Viktor
--Ivanov
0
 
LVL 10

Expert Comment

by:Lischke
ID: 1356078
scrapdoc,

GetDIBits and SetDIBits are to retrieve and set the actual pixel in a Windows bitmap (HBITMAP), while BitBlt serves as general pixel copy (draw) method as shown in the sample by viktornet (btw: victornet, you don't need to retrieve the desktop window, just call DC:=GetDC(0), which does the same as your code).

To use for instance GetDIBits you'd need a HBITMAP (e.g. TBitmap.Handle) and a global memory area allocated by yourself to hold the pixels. You can exactly specify in which format you want to have the data (as stored, in 256 colors, true color etc.).

Consider this code snippet:

type PPixelArray  = ^TByteArray;

var Data        : PPixelArray;
    BMInfo      : TBitmapInfo;
    I,ImageSize : Integer;
    MemDC       : HDC;

begin
  with BMinfo.bmiHeader do
  begin
    // create description of the required image format
    FillChar(BMInfo,SizeOf(BMInfo),0);
    biSize:=sizeof(TBitmapInfoHeader);
    biBitCount:=24;
    biWidth:=Image.Width;
    biHeight:=Image.Height;
    ImageSize:=biWidth*biHeight;
    biPlanes:=1;
    biCompression:=BI_RGB;
    MemDC:=CreateCompatibleDC(0);
    // RGB colors wanted, so triple the pixel count to get the actual size
    Getmem(Data,ImageSize*3);
    try
      // get the actual bits of the image
      GetDIBits(MemDC,Bitmap.Handle,0,biHeight,Data,BMInfo,DIB_RGB_COLORS);
:
:

Here a bitmap info header is prepared which describes the format and image size you want. The memory DC is only used by GetDIBits if you require a palettized image (256 colors or less).

SetDIBits is very similar to GetDIBits while it reverses the direction of the pixel transfer.

Ciao, Mike
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1356079
Mike,

I know that GetDC(0); returns the DC of the desktop, but just decided to do it that way...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:scrapdog
ID: 1356080
Thanks for the info, but I just discovered that I have a problem.  TImage doesn't have a Bitmap property!!  I suppose I should use Image.Picture.Bitmap instead...is this the right way?  All I need is the "Canvas" of the Image in bitmap form for me to do operations on.  Going through the Canvas property is just too slow, that is why I elected to use Get and SetDIBits.

Is Image.Picture.Bitmap the correct way, and if it is, is there some initialization necessary?  (I am building a visual component based on TImage).
0
 
LVL 5

Author Comment

by:scrapdog
ID: 1356081
Note:  Delphi 2.
0
 
LVL 5

Author Comment

by:scrapdog
ID: 1356082
Adjusted points to 50
0
 
LVL 5

Author Comment

by:scrapdog
ID: 1356083
I decided to open up a new question for this:

http://www.experts-exchange.com/topics/comp/lang/delphi/Q.10117264

Please take a look at it.

Vik:  thanks for your help.
Mike:  since I did use your GetDIBits and SetDIBits code, go ahead and lock this.
0
 
LVL 10

Accepted Solution

by:
Lischke earned 200 total points
ID: 1356084
Just as additional comment: The canvas you can use only depends on the format of the pixel you require (as I said 256 colors or less). To avoid dealing with different DCs the Bitmap canvas property is the right one since it contains probably the right palette.

Timage.Picture.Bitmap is only valid when you have loaded a bitmap. If your original image was an icon etc. then Bitmap won't be valid. Keep this in mind...

Ciao, Mike
0
 
LVL 5

Author Comment

by:scrapdog
ID: 1356085
Thanks.  Hopefully you can answer my other question.  By the way, I am dealing with 24-bit color (no palettes).
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

647 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