看看下面的代码!
unit MyCtrl;
interface
uses
Windows, SysUtils, Classes, Graphics, Forms, ZLib;
procedure GetScreen(var MyStream: TMemoryStream);
procedure UnCompressStream(var MyStream: TMemoryStream);
procedure CompressStream(var MyStream: TMemoryStream);
implementation
procedure GetScreen(var MyStream: TMemoryStream);
var
Dc: HDC;
MyCanvas: TCanvas;
MyRect: TRect;
Bmp: TBitmap;
begin
//捕捉全屏幕图像
Bmp:= TBitmap.Create;
Dc := GetWindowDC(0);
MyCanvas := TCanvas.Create;
try
MyCanvas.Handle := Dc;
MyRect:=Rect(0, 0,Screen.Width, Screen.Height);
//图像为 16位真彩色,也可根据实际需要调整
Bmp.PixelFormat := pf16bit;
Bmp.Width := MyRect.Right;
Bmp.Height := MyRect.Bottom;
//捕捉整个屏幕图像
Bmp.Canvas.CopyRect(MyRect, MyCanvas, MyRect);
Bmp.SaveToStream(MYStream);
finally
MyCanvas.Handle := 0;
MyCanvas.Free;
ReleaseDC(0, Dc);
Bmp.Free;
end;
end;
procedure CompressStream(var MyStream: TMemoryStream);
var
SourceStream: TCompressionStream;
DestStream: TMemoryStream;
Count: Integer;
Begin
//压缩图像
//获得图像流的原始尺寸
Count := MyStream.Size;
DestStream := TMemoryStream.Create;
SourceStream:=TCompressionStream.Create(clDefault, DestStream);
Try
//SourceStream中保存着原始的图像流
MyStream.SaveToStream(SourceStream);
//将原始图像流进行压缩, DestStream中保存着压缩后的图像流
SourceStream.Free;
MYStream.Clear;
//写入原始图像的尺寸
MyStream.WriteBuffer(Count, SizeOf(Count));
//写入经过压缩的图像流
MyStream.CopyFrom(DestStream, 0);
//复位流指针
MyStream.Position := 0;
finally
DestStream.Free;
end;
end;
procedure UnCompressStream(var MyStream: TMemoryStream);
var
SourceStream: TDecompressionStream;
Buffer: PChar;
Count: Integer;
Begin
//还原被压缩图像
//从被压缩的图像流中读出原始图像的尺寸
MyStream.ReadBuffer(Count, SizeOf(Count));
//根据图像尺寸大小为将要读入的原始图像流分配内存块
GetMem(Buffer, Count);
SourceStream := TDecompressionStream.Create(MyStream);
Try
//将被压缩的图像流解压缩,然后存入 Buffer内存块中
SourceStream.ReadBuffer(Buffer^, Count);
//将原始图像流保存至 DestStream流中
MyStream.Clear;
MyStream.WriteBuffer(Buffer^, Count);
//复位流指针
MyStream.Position := 0;
finally
FreeMem(Buffer);
end;
end;
end.