博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi中BitBlt函数实现屏幕对象抓图
阅读量:6999 次
发布时间:2019-06-27

本文共 3314 字,大约阅读时间需要 11 分钟。

uses WinTypes, WinProcs, Forms, Controls, Classes, Graphics;

function CaptureScreenRect( ARect: TRect ): TBitmap;
var
ScreenDC: HDC;
begin
Result := TBitmap.Create;
with Result, ARect do
begin
Width := Right - Left;
Height := Bottom - Top;
ScreenDC := GetDC( 0 );
try
BitBlt( Canvas.Handle, 0, 0, Width, Height, ScreenDC,
Left, Top, SRCCOPY );
finally
ReleaseDC( 0, ScreenDC );
end;
end;
end;
思路是这样的
parameter 是一个 TRect, 也就是一个 4 方形,你可以设定,是这样的
TRect defines a rectangle.
Unit
Types
Delphi syntax:
type
TRect = packed record
case Integer of
0: (Left, Top, Right, Bottom: Integer);
1: (TopLeft, BottomRight: TPoint);
end;
返回一个 Bitmap 也就是图像拉
创建一个新的 bitmap instance
HDC 是一个 device context (DC),也就可以利用 BitBlt 把windows 图像转到 bitmap 里了。
完整代码在这里,朋友可以直接调用
unit ScrnCap;
interface
uses WinTypes, WinProcs, Forms, Controls, Classes, Graphics;
function CaptureScreenRect( ARect: TRect ): TBitmap;
function CaptureScreen: TBitmap;
function CaptureClientImage( Control: TControl ): TBitmap;
function CaptureControlImage( Control: TControl ): TBitmap;
function CaptureWindowImage( Wnd: HWND ): TBitmap;
implementation
{==============================================================================}
{ Use this to capture a rectangle on the screen... }
function CaptureScreenRect( ARect: TRect ): TBitmap;
{==============================================================================}
var
ScreenDC: HDC;
begin
Result := TBitmap.Create;
with Result, ARect do
begin
Width := Right - Left;
Height := Bottom - Top;
ScreenDC := GetDC( 0 );
try
BitBlt( Canvas.Handle, 0, 0, Width, Height, ScreenDC,
Left, Top, SRCCOPY );
finally
ReleaseDC( 0, ScreenDC );
end;
end;
end;
{==============================================================================}
{ Use this to capture the entire screen... }
function CaptureScreen: TBitmap;
{==============================================================================}
begin
with Screen do
Result := CaptureScreenRect( Rect( 0, 0, Width, Height ));
end;
{==============================================================================}
{ Use this to capture just the client area of a form or control... }
function CaptureClientImage( Control: TControl ): TBitmap;
{==============================================================================}
begin
with Control, Control.ClientOrigin do
Result := CaptureScreenRect( Bounds( X, Y, ClientWidth,
ClientHeight ));
end;
{==============================================================================}
{ Use this to capture an entire form or control... }
function CaptureControlImage( Control: TControl ): TBitmap;
{==============================================================================}
begin
with Control do
if Parent = nil then
Result := CaptureScreenRect( Bounds( Left, Top, Width,
Height ))
else
with Parent.ClientToScreen( Point( Left, Top )) do
Result := CaptureScreenRect( Bounds( X, Y, Width,
Height ));
end;
{==============================================================================}
{ Use this to capture an entire form or control paased as an API hWnd... }
function CaptureWindowImage( Wnd: HWND ): TBitmap;
{==============================================================================}
var
R: TRect;
begin
GetWindowRect(Wnd, R);
Result := CaptureScreenRect(R);
end;
end.

转载于:https://www.cnblogs.com/Closeyes/p/3234704.html

你可能感兴趣的文章
在无法改动bs架构的基础上,添加新的功能(2) 浏览器
查看>>
Android 应用程序只运行一个实例
查看>>
代码整洁
查看>>
ffmpeg cmd
查看>>
网络监控
查看>>
java创建多线程的两种方法
查看>>
财务收支问题
查看>>
ADF 客户端代码调用服务器方法
查看>>
C++输入cin详解
查看>>
java与openssl的rsa算法互用
查看>>
Python strip lstrip rstrip使用方法
查看>>
Codeforces Round #268 (Div. 2) c
查看>>
如果后台的Activity由于某原因被系统回收了,如何在被系统回收之前保存当前状态?...
查看>>
postgresql 自动备份
查看>>
读写文件之日志文件
查看>>
win7 远程桌面凭证不工作
查看>>
centos 启动多台zookeeper
查看>>
NASA研制3D食物打印机:原料或取自昆虫
查看>>
彭斌_无人机的发展与未来
查看>>
OpenSSLRSAWrapper
查看>>