scrapdog
asked on
GetDIBits, SetDIBits, BitBlt
Someone please give me a simple example (or tutorial) of these functions. I will be using them with TImage.Bitmap.
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(BMI nfo),0);
biSize:=sizeof(TBitmapInfo Header);
biBitCount:=24;
biWidth:=Image.Width;
biHeight:=Image.Height;
ImageSize:=biWidth*biHeigh t;
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.Han dle,0,biHe ight,Data, BMInfo,DIB _RGB_COLOR S);
:
:
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
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(BMI
biSize:=sizeof(TBitmapInfo
biBitCount:=24;
biWidth:=Image.Width;
biHeight:=Image.Height;
ImageSize:=biWidth*biHeigh
biPlanes:=1;
biCompression:=BI_RGB;
MemDC:=CreateCompatibleDC(
// 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.Han
:
:
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
Mike,
I know that GetDC(0); returns the DC of the desktop, but just decided to do it that way...
I know that GetDC(0); returns the DC of the desktop, but just decided to do it that way...
ASKER
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).
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).
ASKER
Note: Delphi 2.
ASKER
Adjusted points to 50
ASKER
I decided to open up a new question for this:
https://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.
https://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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. Hopefully you can answer my other question. By the way, I am dealing with 24-bit color (no palettes).
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
ReleaseDC(H, DC);
end;
This would capture the image of the desktop in your TImage component's bitmap...
-Viktor
--Ivanov