This article describes the method of taking screenshots and saving them to disk in Delphi. Share it with everyone for your reference. The specific analysis is as follows:
This example can capture the screen and save it in JPEG file format.
procedure TForm1.ScreenCap(LeftPos,TopPos,RightPos,BottomPos:integer);var RectWidth,RectHeight:integer; SourceDC,DestDC,Bhandle:integer; Bitmap:TBitmap; MyJpeg: TJpegImage; Stream:TMemoryStream;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; MyJpeg.SaveToFile('C:MyJPEGImage.JPG'); finally MyJpeg.Free; Bitmap.Free; DeleteDC(DestDC); ReleaseDC(Bhandle,SourceDC); end;end;I hope this article will be helpful to everyone's Delphi programming.