Olympus
asked on
Taking a shot of the screen?
How can I take a shot of the screen (by not using a tbitmap component)? I need to do this in a console application where exe size is critical so i am not using and graphic libraries or forms.pas
--Olympus
--Olympus
ASKER
i know, unfortunately i have no points to offer as of yet.
ASKER
Adjusted points from 0 to 50
ASKER
k i just got more, here...
Here's a much better approach than the one suggested:
procedure ScreenShot(x : integer;
y : integer;
Width : integer;
Height : integer;
bm : TBitMap);
var
dc: HDC;
lpPal : PLOGPALETTE;
begin
{test width and height}
if ((Width = 0) OR (Height = 0)) then begin
exit;
end;
bm.Width := Width;
bm.Height := Height;
{get the screen dc}
dc := GetDc(0);
if (dc = 0) then begin
exit;
end;
{do we have a palette device?}
if (GetDeviceCaps(dc, RASTERCAPS) AND
RC_PALETTE = RC_PALETTE) then begin
{allocate memory for a logical palette}
GetMem(lpPal,
sizeof(TLOGPALETTE) +
(255 * sizeof(TPALETTEENTRY)));
{zero it out to be neat}
FillChar(lpPal^,
sizeof(TLOGPALETTE) +
(255 * sizeof(TPALETTEENTRY)),
#0);
{fill in the palette version}
lpPal^.palVersion := $300;
{grab the system palette entries}
lpPal^.palNumEntries :=
GetSystemPaletteEntries(dc ,
0,
256,
lpPal^.palPalEntry);
if (lpPal^.PalNumEntries <> 0) then begin
{create the palette}
bm.Palette := CreatePalette(lpPal^);
end;
FreeMem(lpPal, sizeof(TLOGPALETTE) +
(255 * sizeof(TPALETTEENTRY)));
end;
{copy from the screen to the bitmap}
BitBlt(bm.Canvas.Handle,
0,
0,
Width,
Height,
Dc,
x,
y,
SRCCOPY);
{release the screen dc}
ReleaseDc(0, dc);
end;
This one handles palette entries in a much better fashion than simply capturing HDC(0).
Alex
procedure ScreenShot(x : integer;
y : integer;
Width : integer;
Height : integer;
bm : TBitMap);
var
dc: HDC;
lpPal : PLOGPALETTE;
begin
{test width and height}
if ((Width = 0) OR (Height = 0)) then begin
exit;
end;
bm.Width := Width;
bm.Height := Height;
{get the screen dc}
dc := GetDc(0);
if (dc = 0) then begin
exit;
end;
{do we have a palette device?}
if (GetDeviceCaps(dc, RASTERCAPS) AND
RC_PALETTE = RC_PALETTE) then begin
{allocate memory for a logical palette}
GetMem(lpPal,
sizeof(TLOGPALETTE) +
(255 * sizeof(TPALETTEENTRY)));
{zero it out to be neat}
FillChar(lpPal^,
sizeof(TLOGPALETTE) +
(255 * sizeof(TPALETTEENTRY)),
#0);
{fill in the palette version}
lpPal^.palVersion := $300;
{grab the system palette entries}
lpPal^.palNumEntries :=
GetSystemPaletteEntries(dc
0,
256,
lpPal^.palPalEntry);
if (lpPal^.PalNumEntries <> 0) then begin
{create the palette}
bm.Palette := CreatePalette(lpPal^);
end;
FreeMem(lpPal, sizeof(TLOGPALETTE) +
(255 * sizeof(TPALETTEENTRY)));
end;
{copy from the screen to the bitmap}
BitBlt(bm.Canvas.Handle,
0,
0,
Width,
Height,
Dc,
x,
y,
SRCCOPY);
{release the screen dc}
ReleaseDc(0, dc);
end;
This one handles palette entries in a much better fashion than simply capturing HDC(0).
Alex
listenning
ASKER
"(by not using a tbitmap component)"
Where do you want to store the resulting image then? It must be stored somewhere.
Alex
Alex
ASKER
it can be stored anywhere as long as it doesnt use Forms.Pas or Graphics.pas, my console app cant increase much more in size (its already 119kb)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
as a followup question, how would i do jpeg compression or some sort of bmp compression without using the delphi classes again?
ASKER
and how would i use BitBlt with the windows bitmap?
the first value of it is a handle, what can i put there?
BitBlt(<whatgoeshere?>, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY);
the first value of it is a handle, what can i put there?
BitBlt(<whatgoeshere?>, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY);
as you are a new user you should probably read here:
https://www.experts-exchange.com/info/howto.htm#20