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

限时加码!20+主流AI编程工具免费用 购周边加赠Coding Plan Lite,Claude Code、Cursor等即刻畅享,学习进阶更高效! 阅读详情

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

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.
Delphi之窗的 117.40.118.* 于 2010-6-26 20:12:46 评论道:
谢谢!!!



引文来源   Delphi中BitBlt函数实现屏幕对象抓图 - Delphi之窗
BitBlt方法应用事例 BitBlt方法应用事例 [DllImport("gdi32.dll ")] private static extern bool BitBlt( IntPtr hdcDest, // handle to destination DC int nXDest, // x-coord of destination upper-left corner int nYDest, // y-coord of destination upper-left corner int nWidth, // width of destination rectangle int nHeight, // height of destination rectangle IntPtr hdcSrc, // handle to source DC int nXSrc, // x-coordinate of source upper-left corner int nYSrc, // y-coordinate of source upper-left corner System.Int32 dwRop // raster operation code ); 立即下载

相关推荐

BitBltPic_delphi_

Delphi实现网页焦点图类似的图片动画切换效果

DelphiBitBlt函数屏幕截屏

BitBlt函数的功能是:把源设备的矩形放到目标设备中。让我们来看下函数原型 BOOL BitBlt( HDC hdcDest, // handle to destination device context int nXDest, // x-coordinate of destination rectangle's upper-left corner int nY...

拿头去拼啊 1903

量子通信:初识量子城域网

对量子城域网的概念、作用、拓扑、建设内容等进行了较为全面的初步介绍。

量子领域相关话题 3306

DelphiBitBlt函数实现屏幕对象抓图

 uses WinTypes, WinProcs, Forms, Controls, Classes, Graphics;function CaptureScreenRect( ARect: TRect ): TBitmap;varScreenDC: HDC;beginResult := TBitmap.Create;with Result, ARect dobegin

编程资料收集与共享交流 3378

Delphi API 之 BitBlt

BitBlt函数对指定的源设备环境区域中的像素进行位块(bit_block)转换,以传送到目标设备环境 BitBlt( DestDC: HDC; {目标 DC} X, Y, Width, Height: Integer; {目标位置与大小} SrcDC: HDC; {源 DC} XSrc, YSr...

weixin_30530523的博客 350

Delphi图象截取编程示例(8)

(七)抓取图标(ICON)的体创建一个新的Form3,保存为Capture3.pas。设置属性BorderIcons的四个属性为false.BorderStyle设为bsNone,FormStyle设为fsStayOnTop.一个私有变量:fDragging:Boolean; 两个公共变量:fRect:TRect,fBmp:TBitmap;unit Capture3;interface

迷茫 4051

delphi绘图canvas及应用(转)

<br />delphi绘图canvas及应用2010年03月25日 星期四 14:59DELPHI为编程者提供了一个灵活的绘图场所,CANVAS类,在DELPHI中的很多控件都具有此属性,使编程者可以    <br />在这些控件的表面随心所欲的绘图,这对完善用户界面或者制作一些屏幕特技都有着非凡的作用。 <br />一、CANVAS必备基本知识    <br />1.具有CANVAS属性的控件   <br />TBitmap,TComboBox,TDBComboBox,TDBGrid,TDBListBo

ericalbert的专栏 1万+

Delphi 6 IDE的运行时抓图及简介

LoginCommunity Home > Delphi Delphi 6 Screen Shots: The IDE - by Anders OhlssonAbstract:Screen shots of the Delphi 6 IDEWanna see the Delphi 6 IDE? Splash screen Improved CodeInsight with colors and resizable window (persists of course). Filters as yo

.NET博文收藏 276

delphi抓全屏图,游戏口,游戏Client

以下的抓图,来源于网上。 function dlGetDesktopRect(nLeft,nTop,nWidth,nHeight:integer;pixel:TPixelFormat):TBitmap;var dcDesk:hdc; bmp:TBitmap;begin bmp:=TBitmap.Create; bmp.PixelFormat := pixel; bmp.Width:...

weixin_30462049的博客 155

拷贝对屏幕图象捕获的文章

<br />我们已经了解了Visual Basic或者Delphi等语言是如何来实现屏幕图象捕获的。那么对于C#来说,是如何实现这种功能的?本文就来探讨一下这个问题。<br /><br />一. 程序设计开发及运行环境:<br /><br />  (1).微软视2000服务器版<br /><br />  (2)..Net FrameWork SDK Beta 2<br /><br />二. 程序设计的关键步骤以及具体的实现方法:<br /><br />   (1).首先要创建一个和当前屏幕大小相同的Bi

智慧是做事用的 490

Delphi图象截取编程示例(6)

(六)区域抓图体创建一个新的Form1,保存为Capture1.pas。设置属性BorderIcons的四个属性为false.BorderStyle设为bsNone,Cursor设为crCross,FormStyle设为fsStayOnTop.添加一个私有变量:fDragging:Boolean;两个公共变量:fRect:TRect,fBmp:TBitmap;Form1的作用:在区域抓图时创建,

迷茫 7134

delphi 使用双缓冲画图时的重要拷贝代码

delphi 使用双缓冲画图时的重要拷贝代码

zhangxiaonanwin的专栏 1104
上一篇: 远程接入及应用虚拟化软件稳定性解析_远程接入_综合_eNet硅谷动力资讯频道
下一篇: Windows7新的Taskbar 在 Delphi 2010 中的编程应用
sun280
博客等级 码龄25年 3粉丝 457原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值