• 用Delphi实现抓屏


    //调用方法
    procedure TForm4.BitBtn1Click(Sender: TObject);
    var
      tBM : TBitmap;
    begin
      tBM :
    = TBitmap.Create;
      ScreenShot(
    0,0,Screen.Width,Screen.height,tBM);
      tBM.SaveToFile(
    '.\ScreenShot.BMP');
      tBm.FreeImage;
      FreeAndNil(tBM);
      image1.Picture.LoadFromFile(
    '.\screenshot.bmp');
    end;

    //抓屏函数
    procedure TForm4.ScreenShot(x : integer; y : integer; Width : integer; Height : integer; bm : TBitMap);
    var
      dc: HDC; lpPal : PLOGPALETTE;
    begin
      
    // 检测所需抓屏的区域
      if ((Width = 0) or (Height = 0)) then exit;
      bm.Width :
    = Width;
      bm.Height :
    = Height;
      
    //获取设备上下文
      dc := GetDc(0);
      
    if (dc = 0) then exit;
      {
    do we have a palette device?}
      
    if (GetDeviceCaps(dc, RASTERCAPS) AND
        RC_PALETTE 
    = RC_PALETTE) then
        begin
        {allocate memory 
    for a logical palette}
        GetMem(lpPal, sizeof(TLOGPALETTE) 
    + (255 * sizeof(TPALETTEENTRY)));
        {zero it out to be neat}
        FillChar(lpPal
    ^, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)), #0);
        {fill in the palette version}
        lpPal
    ^.palVersion := $300;
        {grab the system palette entries}
        lpPal
    ^.palNumEntries :=
          GetSystemPaletteEntries(dc,
    0,256,lpPal^.palPalEntry);
        
    if (lpPal^.PalNumEntries <> 0) then
        begin
          {create the palette}
          bm.Palette :
    = CreatePalette(lpPal^);
        end;
        FreeMem(lpPal, sizeof(TLOGPALETTE) 
    + (255 * sizeof(TPALETTEENTRY)));
        end;
      {copy from the screen to the bitmap}
      BitBlt(bm.Canvas.Handle,
    0,0,Width,Height,Dc,x,y,SRCCOPY);
      {release the screen dc}
      ReleaseDc(
    0, dc);
    end;
  • 相关阅读:
    Web 性能优化
    js 校验身份证
    html5 输入框响应enter按键
    获取浏览器的可视窗口宽高
    js打开新标签
    Java数据脱敏框架
    Spring Boot中的事务管理
    运维监控知识体系
    git常用命令图解 & 常见错误
    安全团队不可错过的七个云安全开源工具(转载)
  • 原文地址:https://www.cnblogs.com/taobataoma/p/858251.html
Copyright © 2020-2023  润新知