首页 > 编程知识 正文

Delphi之BitBlt函数与屏幕截屏

时间:2023-05-06 05:34:46 阅读:207286 作者:46

BitBlt函数的功能是:把源设备的矩形放到目标设备中。让我们来看下函数原型

BOOL BitBlt( HDC hdcDest,// handle to destination device context int nXDest,// x-coordinate of destination rectangle's upper-left corner int nYDest,// y-coordinate of destination rectangle's upper-left corner int nWidth,// width of destination rectangle int nHeight,// height of destination rectangle HDC hdcSrc,// handle to source device context int nXSrc,// x-coordinate of source rectangle's upper-left corner int nYSrc,// y-coordinate of source rectangle's upper-left corner DWORD dwRop // raster operation code );

hdcDest 就是目标设备句柄

nXDest 目标设备的X

nYDest 目标设备的Y

nWidth 目标设备的宽

nHeight 目标设备的高

hdcSrc 源设备句柄

nXSrc 源矩形的起始x

nYSrc 源矩形的起始y

dwRop 光栅操作码, 也就是源像素与目标像素的混合方式

下面是所有的操作码,没错都是英文的,等有功夫我再翻译过来,这里就先翻译两个我现在要用到的。是SRCCOPY。

值       说明

SRCCOPY   把源矩形直接复制到目标矩形中
BLACKNESS    Fills the destination rectangle using the color associated with index 0 in the physical palette. (This color is black for the default physical palette.)
DSTINVERT    Inverts the destination rectangle.
MERGECOPY    Merges the colors of the source rectangle with the specified pattern by using the Boolean AND operator.
MERGEPAINT    Merges the colors of the inverted source rectangle with the colors of the destination rectangle by using the Boolean OR operator.
NOTSRCCOPY    Copies the inverted source rectangle to the destination.
NOTSRCERASE    Combines the colors of the source and destination rectangles by using the Boolean OR operator and then inverts the resultant color.
PATCOPY    Copies the specified pattern into the destination bitmap.
PATINVERT    Combines the colors of the specified pattern with the colors of the destination rectangle by using the Boolean XOR operator.
PATPAINT    Combines the colors of the pattern with the colors of the inverted source rectangle by using the Boolean OR operator. The result of this operation is combined with the colors of the destination rectangle by using the Boolean OR operator.
SRCAND    Combines the colors of the source and destination rectangles by using the Boolean AND operator.

SRCERASE    Combines the inverted colors of the destination rectangle with the colors of the source rectangle by using the Boolean AND operator.
SRCINVERT    Combines the colors of the source and destination rectangles by using the Boolean XOR operator.
SRCPAINT    Combines the colors of the source and destination rectangles by using the Boolean OR operator.
WHITENESS    Fills the destination rectangle using the color associated with index 1 in the physical palette. (This color is white for the default physical palette.)

 

下面是截屏代码,如果有些函数或者结构体难理解的 我会再开一篇文章专门记录,以便自己可以查询使用

unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;const ALPHA_SCREEN = 255; { 背景ALPHA值 }type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormPaint(Sender: TObject); private FBF: TBlendFunction; { Alpha TBlendFunction结构 } FScreenBitMap:TBitmap; procedure SetAlpha(const ARect: TRect; { 设置透明 } const AlphaValue: Byte = 255); public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);const CAPTUREBLT = $40000000;var LDC:HDC;//源句柄begin FormStyle:=fsStayOnTop;//窗体始终位于最前面 BorderStyle:=bsNone;//窗体无边框 WindowState:=wsMaximized;//窗体最大化 Color:=clBlack;//设置窗体背景颜色为黑色 FScreenBitMap:=TBitMap.Create;//创建原图 BoundsRect := Screen.DesktopRect; //BoundsRect就是一个Rect 后面是获取桌面的Rect //获取桌面图 LDC:=GetDC(GetDesktopWindow);//获取桌面句柄 FScreenBitMap.Width:=Width;//和桌面宽度一样 这里后面的Width是窗口宽,但是窗口最大化了所以是桌面宽 FScreenBitMap.Height:=Height;//和桌面高度一样 BitBlt(FScreenBitMap.Canvas.Handle,0,0,Width,Height,LDC,0,0,SRCCOPY or CAPTUREBLT); ReleaseDC(Handle,LDC);end;procedure TForm1.SetAlpha(const ARect: TRect; const AlphaValue: Byte = 255);begin with FBF do begin BlendOp := AC_SRC_OVER; BlendFlags := 0; { 0 } SourceConstantAlpha := AlphaValue; { 透明度: 0..255 } AlphaFormat := 0; end; with ARect do Windows.AlphaBlend(Canvas.Handle, Left, Top, Right - Left, Bottom - Top, FScreenBitMap.Canvas.Handle, Left, Top, Right - Left, Bottom - Top, FBF);(*AlphaBled原型BOOL AlphaBlend( HDC hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, BLENDFUNCTION blendFunction );这前面的和BitBlt一样 都是目标DC和源DC和他们的位置宽高,最后一个是混合函数,混合函数是上面的FBF,已经赋值好了 FBF 的结构 以及注释如下*) (* TBlendFunction = _BLENDFUNCTION;_BLENDFUNCTION = packed record BlendOp: BYTE;//指定源混合操作,目前,唯一的源和目标的混合方式已定义为AC_SRC_OVER BlendFlags: BYTE;//必须是0 SourceConstantAlpha: BYTE;//指定一个alpha透明度值,这个值将用于整个源位图;该SourceConstantAlpha值与源位图的每个像素的alpha值组合;如果设置为0,就会假定你的图片是透明的;如果需要使用每像素本身的alpha值,设置SourceConstantAlpha值255(不透明) AlphaFormat: BYTE;//这个参数控制源和目标的解析方式,AlphaFormat参数有以下值: AC_SRC_ALPHA:这个值在源或者目标本身有Alpha通道时(也就是操作的图本身带有透明通道信息时),提醒系统API调用函数必须预先乘以alpha值, 也就是说位图上某个像素位置的red、green、blue通道值必须先与alpha相乘。例如,如果alpha透明值是x,那么red、green、blue三个通道的值 必须乘以x并且再除以255(因为alpha的值的范围时0~255),之后才能被调用。 备注: 1.当AlphaFormat参数的值是AC_SRC_ALPHA,那么源位图必须是32位深,否则的话,AlphaBland函数将调用失败 2.当BlendOp参数是AC_SRC_OVER时,源位图根据alpha透明度值直接覆盖在目标位图之上 3.如果源位图不带有透明度信息(那样的话,AC_SRC_ALPHA不设置),将由SourceConstantAplha的值来决定如何混合源位图与目标位图 4.如果源位图没有用SourceConstantAlpha参数值(那表示该参数等于255),每一个像素的透明度将决定源位图和目标位图的混合结果 5.如果源位图既有SourceConstantAlpha值(也就是它的值不是255),每个像素又有透明度值,那么源位图的每一个像素将首先乘以 SourceConstantAlpha的值,然后根据每个像素的透明度值混合。 end; *)end;procedure TForm1.FormPaint(Sender: TObject);begin SetAlpha(BoundsRect, ALPHA_SCREEN);end;end.

对于上面透明的这个函数有一个很好的帖子可以看下,下面贴上地址

https://blog.csdn.net/iteye_6637/article/details/82536081

运行之后,桌面图像就被放到窗口里了 如果想要保存或者其他,可以去放到Image 再把Image另存为什么都行,不再过多赘述

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。