Screenshot: BitBlt and TCanvas.CopyRect. There are others WinApi equivalent to these two functions?












0















Exists others functions of Windows api equivalent to BitBlt and TCanvas.CopyRect and that can be used in a screen capture procedure?



Here are two codes that are able to produce screenshot using BitBl and TCanvas.CopyRect respectively:



procedure GetScreenToBmp(StreamName: TMemoryStream);
var
Rect: Windows.TRect;
DC, bitmapDC, windowDC: HDC;
W, H: Integer;
BI: BITMAPINFO;
bitmap: HBITMAP;
bmi: Windows.bitmap;
ColorDepth: Integer;
bitmapData: Pointer;
BMP: TBitmap;
Begin
GetWindowRect(GetDesktopWindow, Rect);

DC := GetDC(GetDesktopWindow);
Try
ColorDepth := GetDeviceCaps(DC, BitsPixel);
Finally
ReleaseDC(GetDesktopWindow, DC);
End;

W := Rect.Right - Rect.Left;
H := Rect.Bottom - Rect.Top;

bitmapDC := CreateCompatibleDC(0);
Try

BI.bmiHeader.biSize := SizeOf(BITMAPINFOHEADER);
BI.bmiHeader.biWidth := W;
BI.bmiHeader.biHeight := H;
BI.bmiHeader.biPlanes := 1;
BI.bmiHeader.biBitCount := ColorDepth;
BI.bmiHeader.biCompression := BI_RGB;
BI.bmiHeader.biSizeImage := W * H * ColorDepth Div 8;
BI.bmiHeader.biXPelsPerMeter := 100;
BI.bmiHeader.biYPelsPerMeter := 100;
BI.bmiHeader.biClrUsed := 0;
BI.bmiHeader.biClrImportant := 0;

bitmap := CreateDIBSection(bitmapDC, BI, DIB_RGB_COLORS, bitmapData, 0, 0);
If bitmap = 0 Then
Exit;

GetObject(bitmap, SizeOf(bmi), @bmi);

Finally
DeleteDC(bitmapDC);
End;

bitmapDC := CreateCompatibleDC(0);
Try
SelectObject(bitmapDC, bitmap);
windowDC := GetWindowDC(GetDesktopWindow);
Try
BitBlt(bitmapDC, 0, 0, W, H, windowDC, 0, 0, SRCCOPY);
Finally
ReleaseDC(GetDesktopWindow, windowDC);
End;

Finally
DeleteDC(bitmapDC);
End;

BMP := TBitmap.Create;
Try
BMP.ReleaseHandle;
BMP.ReleaseMaskHandle;
BMP.ReleasePalette;
BMP.PixelFormat := pf24bit;
BMP.Handle := bitmap;
BMP.SaveToFile('test.bmp');
//BMP.SaveToStream(StreamName);
Finally
BMP.Free;
End;
End;

procedure GetScreenToBmp2(StreamName: TMemoryStream);
var
Mybmp: TBitmap;
DC: HDC;
Mycan: Tcanvas;
R: TRect;
begin
Mybmp := TBitmap.Create;
Mycan := Tcanvas.Create;
DC := GetWindowDC(0);
try
Mycan.Handle := DC;
R := Rect(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
Mybmp.Width := R.Right;
Mybmp.Height := R.Bottom;
Mybmp.Canvas.CopyRect(R, Mycan, R);
finally
ReleaseDC(0, DC);
end;
Mycan.Handle := 0;
Mycan.Free;

Mybmp.PixelFormat := pf24bit;
Mybmp.SaveToFile('test.bmp');
//Mybmp.SaveToStream(StreamName);
Mybmp.Free;
end;









share|improve this question


















  • 1





    Just for fun or you need the alternative to be better in some aspect?

    – Sertac Akyuz
    Nov 25 '18 at 19:46











  • To bypass api hook in BitBlt, StretchBlt for example.

    – Davison
    Nov 25 '18 at 19:48






  • 3





    There are other ways to capture, not necessarily through one api function though. Some.

    – Sertac Akyuz
    Nov 25 '18 at 19:51











  • Yes, i also already saw this link, but all suggestions are covered by api hook in BitBlt, StretchBlt. I already tested all these suggestions.

    – Davison
    Nov 25 '18 at 19:59








  • 3





    TCanvas.CopyRect() is just a thin wrapper for StretchBlt(). BitBlt() and StretchBlt() are Win32 API functions, not a VCL functions. If someone decides to hook the Win32 API directly, there is really not much you can do about it, short of manually removing the hooks, or writing a custom display driver. Why do you need to avoid the hooks? Is someone altering the screenshots you are trying to take?

    – Remy Lebeau
    Nov 25 '18 at 21:05
















0















Exists others functions of Windows api equivalent to BitBlt and TCanvas.CopyRect and that can be used in a screen capture procedure?



Here are two codes that are able to produce screenshot using BitBl and TCanvas.CopyRect respectively:



procedure GetScreenToBmp(StreamName: TMemoryStream);
var
Rect: Windows.TRect;
DC, bitmapDC, windowDC: HDC;
W, H: Integer;
BI: BITMAPINFO;
bitmap: HBITMAP;
bmi: Windows.bitmap;
ColorDepth: Integer;
bitmapData: Pointer;
BMP: TBitmap;
Begin
GetWindowRect(GetDesktopWindow, Rect);

DC := GetDC(GetDesktopWindow);
Try
ColorDepth := GetDeviceCaps(DC, BitsPixel);
Finally
ReleaseDC(GetDesktopWindow, DC);
End;

W := Rect.Right - Rect.Left;
H := Rect.Bottom - Rect.Top;

bitmapDC := CreateCompatibleDC(0);
Try

BI.bmiHeader.biSize := SizeOf(BITMAPINFOHEADER);
BI.bmiHeader.biWidth := W;
BI.bmiHeader.biHeight := H;
BI.bmiHeader.biPlanes := 1;
BI.bmiHeader.biBitCount := ColorDepth;
BI.bmiHeader.biCompression := BI_RGB;
BI.bmiHeader.biSizeImage := W * H * ColorDepth Div 8;
BI.bmiHeader.biXPelsPerMeter := 100;
BI.bmiHeader.biYPelsPerMeter := 100;
BI.bmiHeader.biClrUsed := 0;
BI.bmiHeader.biClrImportant := 0;

bitmap := CreateDIBSection(bitmapDC, BI, DIB_RGB_COLORS, bitmapData, 0, 0);
If bitmap = 0 Then
Exit;

GetObject(bitmap, SizeOf(bmi), @bmi);

Finally
DeleteDC(bitmapDC);
End;

bitmapDC := CreateCompatibleDC(0);
Try
SelectObject(bitmapDC, bitmap);
windowDC := GetWindowDC(GetDesktopWindow);
Try
BitBlt(bitmapDC, 0, 0, W, H, windowDC, 0, 0, SRCCOPY);
Finally
ReleaseDC(GetDesktopWindow, windowDC);
End;

Finally
DeleteDC(bitmapDC);
End;

BMP := TBitmap.Create;
Try
BMP.ReleaseHandle;
BMP.ReleaseMaskHandle;
BMP.ReleasePalette;
BMP.PixelFormat := pf24bit;
BMP.Handle := bitmap;
BMP.SaveToFile('test.bmp');
//BMP.SaveToStream(StreamName);
Finally
BMP.Free;
End;
End;

procedure GetScreenToBmp2(StreamName: TMemoryStream);
var
Mybmp: TBitmap;
DC: HDC;
Mycan: Tcanvas;
R: TRect;
begin
Mybmp := TBitmap.Create;
Mycan := Tcanvas.Create;
DC := GetWindowDC(0);
try
Mycan.Handle := DC;
R := Rect(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
Mybmp.Width := R.Right;
Mybmp.Height := R.Bottom;
Mybmp.Canvas.CopyRect(R, Mycan, R);
finally
ReleaseDC(0, DC);
end;
Mycan.Handle := 0;
Mycan.Free;

Mybmp.PixelFormat := pf24bit;
Mybmp.SaveToFile('test.bmp');
//Mybmp.SaveToStream(StreamName);
Mybmp.Free;
end;









share|improve this question


















  • 1





    Just for fun or you need the alternative to be better in some aspect?

    – Sertac Akyuz
    Nov 25 '18 at 19:46











  • To bypass api hook in BitBlt, StretchBlt for example.

    – Davison
    Nov 25 '18 at 19:48






  • 3





    There are other ways to capture, not necessarily through one api function though. Some.

    – Sertac Akyuz
    Nov 25 '18 at 19:51











  • Yes, i also already saw this link, but all suggestions are covered by api hook in BitBlt, StretchBlt. I already tested all these suggestions.

    – Davison
    Nov 25 '18 at 19:59








  • 3





    TCanvas.CopyRect() is just a thin wrapper for StretchBlt(). BitBlt() and StretchBlt() are Win32 API functions, not a VCL functions. If someone decides to hook the Win32 API directly, there is really not much you can do about it, short of manually removing the hooks, or writing a custom display driver. Why do you need to avoid the hooks? Is someone altering the screenshots you are trying to take?

    – Remy Lebeau
    Nov 25 '18 at 21:05














0












0








0








Exists others functions of Windows api equivalent to BitBlt and TCanvas.CopyRect and that can be used in a screen capture procedure?



Here are two codes that are able to produce screenshot using BitBl and TCanvas.CopyRect respectively:



procedure GetScreenToBmp(StreamName: TMemoryStream);
var
Rect: Windows.TRect;
DC, bitmapDC, windowDC: HDC;
W, H: Integer;
BI: BITMAPINFO;
bitmap: HBITMAP;
bmi: Windows.bitmap;
ColorDepth: Integer;
bitmapData: Pointer;
BMP: TBitmap;
Begin
GetWindowRect(GetDesktopWindow, Rect);

DC := GetDC(GetDesktopWindow);
Try
ColorDepth := GetDeviceCaps(DC, BitsPixel);
Finally
ReleaseDC(GetDesktopWindow, DC);
End;

W := Rect.Right - Rect.Left;
H := Rect.Bottom - Rect.Top;

bitmapDC := CreateCompatibleDC(0);
Try

BI.bmiHeader.biSize := SizeOf(BITMAPINFOHEADER);
BI.bmiHeader.biWidth := W;
BI.bmiHeader.biHeight := H;
BI.bmiHeader.biPlanes := 1;
BI.bmiHeader.biBitCount := ColorDepth;
BI.bmiHeader.biCompression := BI_RGB;
BI.bmiHeader.biSizeImage := W * H * ColorDepth Div 8;
BI.bmiHeader.biXPelsPerMeter := 100;
BI.bmiHeader.biYPelsPerMeter := 100;
BI.bmiHeader.biClrUsed := 0;
BI.bmiHeader.biClrImportant := 0;

bitmap := CreateDIBSection(bitmapDC, BI, DIB_RGB_COLORS, bitmapData, 0, 0);
If bitmap = 0 Then
Exit;

GetObject(bitmap, SizeOf(bmi), @bmi);

Finally
DeleteDC(bitmapDC);
End;

bitmapDC := CreateCompatibleDC(0);
Try
SelectObject(bitmapDC, bitmap);
windowDC := GetWindowDC(GetDesktopWindow);
Try
BitBlt(bitmapDC, 0, 0, W, H, windowDC, 0, 0, SRCCOPY);
Finally
ReleaseDC(GetDesktopWindow, windowDC);
End;

Finally
DeleteDC(bitmapDC);
End;

BMP := TBitmap.Create;
Try
BMP.ReleaseHandle;
BMP.ReleaseMaskHandle;
BMP.ReleasePalette;
BMP.PixelFormat := pf24bit;
BMP.Handle := bitmap;
BMP.SaveToFile('test.bmp');
//BMP.SaveToStream(StreamName);
Finally
BMP.Free;
End;
End;

procedure GetScreenToBmp2(StreamName: TMemoryStream);
var
Mybmp: TBitmap;
DC: HDC;
Mycan: Tcanvas;
R: TRect;
begin
Mybmp := TBitmap.Create;
Mycan := Tcanvas.Create;
DC := GetWindowDC(0);
try
Mycan.Handle := DC;
R := Rect(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
Mybmp.Width := R.Right;
Mybmp.Height := R.Bottom;
Mybmp.Canvas.CopyRect(R, Mycan, R);
finally
ReleaseDC(0, DC);
end;
Mycan.Handle := 0;
Mycan.Free;

Mybmp.PixelFormat := pf24bit;
Mybmp.SaveToFile('test.bmp');
//Mybmp.SaveToStream(StreamName);
Mybmp.Free;
end;









share|improve this question














Exists others functions of Windows api equivalent to BitBlt and TCanvas.CopyRect and that can be used in a screen capture procedure?



Here are two codes that are able to produce screenshot using BitBl and TCanvas.CopyRect respectively:



procedure GetScreenToBmp(StreamName: TMemoryStream);
var
Rect: Windows.TRect;
DC, bitmapDC, windowDC: HDC;
W, H: Integer;
BI: BITMAPINFO;
bitmap: HBITMAP;
bmi: Windows.bitmap;
ColorDepth: Integer;
bitmapData: Pointer;
BMP: TBitmap;
Begin
GetWindowRect(GetDesktopWindow, Rect);

DC := GetDC(GetDesktopWindow);
Try
ColorDepth := GetDeviceCaps(DC, BitsPixel);
Finally
ReleaseDC(GetDesktopWindow, DC);
End;

W := Rect.Right - Rect.Left;
H := Rect.Bottom - Rect.Top;

bitmapDC := CreateCompatibleDC(0);
Try

BI.bmiHeader.biSize := SizeOf(BITMAPINFOHEADER);
BI.bmiHeader.biWidth := W;
BI.bmiHeader.biHeight := H;
BI.bmiHeader.biPlanes := 1;
BI.bmiHeader.biBitCount := ColorDepth;
BI.bmiHeader.biCompression := BI_RGB;
BI.bmiHeader.biSizeImage := W * H * ColorDepth Div 8;
BI.bmiHeader.biXPelsPerMeter := 100;
BI.bmiHeader.biYPelsPerMeter := 100;
BI.bmiHeader.biClrUsed := 0;
BI.bmiHeader.biClrImportant := 0;

bitmap := CreateDIBSection(bitmapDC, BI, DIB_RGB_COLORS, bitmapData, 0, 0);
If bitmap = 0 Then
Exit;

GetObject(bitmap, SizeOf(bmi), @bmi);

Finally
DeleteDC(bitmapDC);
End;

bitmapDC := CreateCompatibleDC(0);
Try
SelectObject(bitmapDC, bitmap);
windowDC := GetWindowDC(GetDesktopWindow);
Try
BitBlt(bitmapDC, 0, 0, W, H, windowDC, 0, 0, SRCCOPY);
Finally
ReleaseDC(GetDesktopWindow, windowDC);
End;

Finally
DeleteDC(bitmapDC);
End;

BMP := TBitmap.Create;
Try
BMP.ReleaseHandle;
BMP.ReleaseMaskHandle;
BMP.ReleasePalette;
BMP.PixelFormat := pf24bit;
BMP.Handle := bitmap;
BMP.SaveToFile('test.bmp');
//BMP.SaveToStream(StreamName);
Finally
BMP.Free;
End;
End;

procedure GetScreenToBmp2(StreamName: TMemoryStream);
var
Mybmp: TBitmap;
DC: HDC;
Mycan: Tcanvas;
R: TRect;
begin
Mybmp := TBitmap.Create;
Mycan := Tcanvas.Create;
DC := GetWindowDC(0);
try
Mycan.Handle := DC;
R := Rect(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
Mybmp.Width := R.Right;
Mybmp.Height := R.Bottom;
Mybmp.Canvas.CopyRect(R, Mycan, R);
finally
ReleaseDC(0, DC);
end;
Mycan.Handle := 0;
Mycan.Free;

Mybmp.PixelFormat := pf24bit;
Mybmp.SaveToFile('test.bmp');
//Mybmp.SaveToStream(StreamName);
Mybmp.Free;
end;






windows delphi winapi screenshot delphi-xe7






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 19:39









DavisonDavison

1158




1158








  • 1





    Just for fun or you need the alternative to be better in some aspect?

    – Sertac Akyuz
    Nov 25 '18 at 19:46











  • To bypass api hook in BitBlt, StretchBlt for example.

    – Davison
    Nov 25 '18 at 19:48






  • 3





    There are other ways to capture, not necessarily through one api function though. Some.

    – Sertac Akyuz
    Nov 25 '18 at 19:51











  • Yes, i also already saw this link, but all suggestions are covered by api hook in BitBlt, StretchBlt. I already tested all these suggestions.

    – Davison
    Nov 25 '18 at 19:59








  • 3





    TCanvas.CopyRect() is just a thin wrapper for StretchBlt(). BitBlt() and StretchBlt() are Win32 API functions, not a VCL functions. If someone decides to hook the Win32 API directly, there is really not much you can do about it, short of manually removing the hooks, or writing a custom display driver. Why do you need to avoid the hooks? Is someone altering the screenshots you are trying to take?

    – Remy Lebeau
    Nov 25 '18 at 21:05














  • 1





    Just for fun or you need the alternative to be better in some aspect?

    – Sertac Akyuz
    Nov 25 '18 at 19:46











  • To bypass api hook in BitBlt, StretchBlt for example.

    – Davison
    Nov 25 '18 at 19:48






  • 3





    There are other ways to capture, not necessarily through one api function though. Some.

    – Sertac Akyuz
    Nov 25 '18 at 19:51











  • Yes, i also already saw this link, but all suggestions are covered by api hook in BitBlt, StretchBlt. I already tested all these suggestions.

    – Davison
    Nov 25 '18 at 19:59








  • 3





    TCanvas.CopyRect() is just a thin wrapper for StretchBlt(). BitBlt() and StretchBlt() are Win32 API functions, not a VCL functions. If someone decides to hook the Win32 API directly, there is really not much you can do about it, short of manually removing the hooks, or writing a custom display driver. Why do you need to avoid the hooks? Is someone altering the screenshots you are trying to take?

    – Remy Lebeau
    Nov 25 '18 at 21:05








1




1





Just for fun or you need the alternative to be better in some aspect?

– Sertac Akyuz
Nov 25 '18 at 19:46





Just for fun or you need the alternative to be better in some aspect?

– Sertac Akyuz
Nov 25 '18 at 19:46













To bypass api hook in BitBlt, StretchBlt for example.

– Davison
Nov 25 '18 at 19:48





To bypass api hook in BitBlt, StretchBlt for example.

– Davison
Nov 25 '18 at 19:48




3




3





There are other ways to capture, not necessarily through one api function though. Some.

– Sertac Akyuz
Nov 25 '18 at 19:51





There are other ways to capture, not necessarily through one api function though. Some.

– Sertac Akyuz
Nov 25 '18 at 19:51













Yes, i also already saw this link, but all suggestions are covered by api hook in BitBlt, StretchBlt. I already tested all these suggestions.

– Davison
Nov 25 '18 at 19:59







Yes, i also already saw this link, but all suggestions are covered by api hook in BitBlt, StretchBlt. I already tested all these suggestions.

– Davison
Nov 25 '18 at 19:59






3




3





TCanvas.CopyRect() is just a thin wrapper for StretchBlt(). BitBlt() and StretchBlt() are Win32 API functions, not a VCL functions. If someone decides to hook the Win32 API directly, there is really not much you can do about it, short of manually removing the hooks, or writing a custom display driver. Why do you need to avoid the hooks? Is someone altering the screenshots you are trying to take?

– Remy Lebeau
Nov 25 '18 at 21:05





TCanvas.CopyRect() is just a thin wrapper for StretchBlt(). BitBlt() and StretchBlt() are Win32 API functions, not a VCL functions. If someone decides to hook the Win32 API directly, there is really not much you can do about it, short of manually removing the hooks, or writing a custom display driver. Why do you need to avoid the hooks? Is someone altering the screenshots you are trying to take?

– Remy Lebeau
Nov 25 '18 at 21:05












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471163%2fscreenshot-bitblt-and-tcanvas-copyrect-there-are-others-winapi-equivalent-to-t%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471163%2fscreenshot-bitblt-and-tcanvas-copyrect-there-are-others-winapi-equivalent-to-t%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks