Delphi之BitBlt函數與屏幕截屏

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;

interface

uses
  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另存爲什麼都行,不再過多贅述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章