截取屏幕,并保存为JPEG文件格式
1 using jpeg,ExtCtrls;
2 procedure TForm1.ScreenCap(LeftPos,TopPos,RightPos,BottomPos:integer);
3 var
4 RectWidth,RectHeight:integer;
5 SourceDC,DestDC,Bhandle:integer;
6 Bitmap:TBitmap;
7 MyJpeg: TJpegImage;
8 Stream:TMemoryStream;
9 begin
10 MyJpeg:= TJpegImage.Create;
11 RectWidth:=RightPos-LeftPos;
12 RectHeight:=BottomPos-TopPos;
13 SourceDC:=CreateDC('DISPLAY','','',nil);
14 DestDC:=CreateCompatibleDC(SourceDC);
15 Bhandle:=CreateCompatibleBitmap(SourceDC,
16 RectWidth,RectHeight);
17 SelectObject(DestDC,Bhandle);
18 BitBlt(DestDC,0,0,RectWidth,RectHeight,SourceDC,
19 LeftPos,TopPos,SRCCOPY);
20 Bitmap:=TBitmap.Create;
21 Bitmap.Handle:=BHandle;
22 Stream := TMemoryStream.Create;
23 Bitmap.SaveToStream(Stream);
24 Stream.Free;
25 try
26 MyJpeg.Assign(Bitmap);
27 MyJpeg.CompressionQuality:=70;
28 MyJpeg.Compress;
29 MyJpeg.SaveToFile('C:\MyJPEGImage.JPG');
30 finally
31 MyJpeg.Free;
32 Bitmap.Free;
33 DeleteDC(DestDC);
34 ReleaseDC(Bhandle,SourceDC);
35 end;
36 end;
2 procedure TForm1.ScreenCap(LeftPos,TopPos,RightPos,BottomPos:integer);
3 var
4 RectWidth,RectHeight:integer;
5 SourceDC,DestDC,Bhandle:integer;
6 Bitmap:TBitmap;
7 MyJpeg: TJpegImage;
8 Stream:TMemoryStream;
9 begin
10 MyJpeg:= TJpegImage.Create;
11 RectWidth:=RightPos-LeftPos;
12 RectHeight:=BottomPos-TopPos;
13 SourceDC:=CreateDC('DISPLAY','','',nil);
14 DestDC:=CreateCompatibleDC(SourceDC);
15 Bhandle:=CreateCompatibleBitmap(SourceDC,
16 RectWidth,RectHeight);
17 SelectObject(DestDC,Bhandle);
18 BitBlt(DestDC,0,0,RectWidth,RectHeight,SourceDC,
19 LeftPos,TopPos,SRCCOPY);
20 Bitmap:=TBitmap.Create;
21 Bitmap.Handle:=BHandle;
22 Stream := TMemoryStream.Create;
23 Bitmap.SaveToStream(Stream);
24 Stream.Free;
25 try
26 MyJpeg.Assign(Bitmap);
27 MyJpeg.CompressionQuality:=70;
28 MyJpeg.Compress;
29 MyJpeg.SaveToFile('C:\MyJPEGImage.JPG');
30 finally
31 MyJpeg.Free;
32 Bitmap.Free;
33 DeleteDC(DestDC);
34 ReleaseDC(Bhandle,SourceDC);
35 end;
36 end;