• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi中BitBlt函数实现屏幕对象抓图

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
delphi读写文本文件(函数比较全)发布时间:2022-07-18
下一篇:
基于matlab对ECG信号进行滤波处理发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap