最新消息:网站改版咯

Delphi实现截屏函数

手记本 Yovae 1737浏览

这里需要使用Jpeg类,在程序头部 uses Jpeg;用delphi实现屏幕截屏,以下是函数的实现方法

 
  
  ** 功能: 截取屏幕中的指定区域
  ** 输入参数:
  **     LeftPos :左坐标
  **     TopPos :上坐标
  **         RightPos:右坐标
  **         BottomPos :底部坐标
  ** author : yovae
  ***********************************
procedure ScreenCap(LeftPos,TopPos,RightPos,BottomPos:integer);
var
  RectWidth,RectHeight:integer;
  SourceDC,DestDC,Bhandle:integer;
  Bitmap:TBitmap;
  MyJpeg: TJpegImage;
  Stream:TMemoryStream;
  currentTime:Integer;
begin
  MyJpeg:= TJpegImage.Create;
  RectWidth:=RightPos-LeftPos;
  RectHeight:=BottomPos-TopPos;
  SourceDC:=CreateDC('DISPLAY','','',nil);
  DestDC:=CreateCompatibleDC(SourceDC);
  Bhandle:=CreateCompatibleBitmap(SourceDC,
  RectWidth,RectHeight);
  SelectObject(DestDC,Bhandle);
  BitBlt(DestDC,0,0,RectWidth,RectHeight,SourceDC,
  LeftPos,TopPos,SRCCOPY);
  Bitmap:=TBitmap.Create;
  Bitmap.Handle:=BHandle;
  Stream := TMemoryStream.Create;
  Bitmap.SaveToStream(Stream);
  Stream.Free;
  try
    MyJpeg.Assign(Bitmap);
    MyJpeg.CompressionQuality:=70;
    MyJpeg.Compress;
    currentTime:=DateTimeToUnix(Now);
    MyJpeg.SaveToFile('D:\'+inttostr(currentTime)+'.JPG');
  finally
    MyJpeg.Free;
    Bitmap.Free;
    DeleteDC(DestDC);
    ReleaseDC(Bhandle,SourceDC);
  end;
end;

转载请注明:Yovae Studio » Delphi实现截屏函数