Current location - Loan Platform Complete Network - Foreign exchange account opening - Application of Delphi compressed stream and decompressed stream
Application of Delphi compressed stream and decompressed stream
Software developers will inevitably encounter the problem of compressing data! Friends who often use Delphi know that it provides us with two stream classes (TCompressionStream and TDecompressionStream) to compress and decompress data, but the fly in the ointment is that streams is not explained in detail with the help of Delphi, which makes them difficult to use. In fact, the source codes and libraries of these two classes are provided by Delphi system. It is stored in the directories of InfoExtraslib Src and InfoExtraslibObj of Delphi CD (the inventory is stored in the Obj directory, and the source code is stored in the Src directory, so interested friends can have a look). I got to know them in the process of using them.

Description of this class

1, base class tcustomzlibstream: class tcustomzlibstream is the base class of classes TCompressionStream and TDecompressionStream, and it mainly has an attribute: OnProgress, which will occur during the compression or decompression of classes. It is defined as follows:

Procedure OnProgress (sender: toobject); Dynamic;

2. Compression class TCCompressionstream: In addition to inheriting the OnProgress attribute of the base class, the TCCompressionstream class also adds another attribute: CompressionRate, which is defined as follows:

Property CompressionRate: read GetCompressionRate once. Through this property, you can get the compression rate.

Its several important methods are defined as follows:

Constructor TCompressionStream create(compression level:TCompressionLevel; dest:TStream);

Where: TcompressionLevel (compression type), which is defined by the following:

(1) clNone: no data compression;

(2)cl fast: fast compression at the expense of compression efficiency;

③ clDefault: normal compression;

(4) clMax: maximize compression and sacrifice speed;

Dest: destination stream, used to store compressed data.

Function TCompressionStream. Write (const buffer count: longint): longint;

Where: Buffer: data to be compressed;

Count: the number of bytes of data to be compressed;

Function returns the number of bytes written to the stream.

Only data of compressed class TCompressionStream can be written. If you try to read data from it, an "error" exception will appear. The data to be compressed is written into the stream through the Write method, compressed during the writing process, stored in the TmemoryStream provided by the constructor, and the OnProcess event is triggered at the same time.

3. Decompression class TDecompressionStream: Contrary to compression class TcompressionStream, its data can only be read. If you try to write data to it, an "error" exception will appear. Its several important methods are defined as follows:

Constructor create (source: tstream);

Where: Source is the stream for saving compressed data;

Function reading (var buffer; count:Longint):Longint;

Data reading function, buffer: data storage buffer;

Count: the size of the buffer;

Function returns the number of bytes read.

In the process of reading data, the data is decompressed and the OnProcess event is triggered.

Second, the use of classes

Through the cooperation of TCompressionStream and TdecompressionStream, we can compress and decompress data very conveniently. The following is an example I used when writing a screen copying program:

Process TClientForm GetScreen

defined variable

SourceDC,dest DC:HDC;

bhandle:hbit map;

Bitmap: TBitMap

BmpStream,dest stream:TMemoryStream;

source stream:TCompressionStream;

Count: integer;

begin

SourceDC:=CreateDC('display ','','',nil);

{get the DC of the screen}

DestDC:= CreateCompatibleDC(SourceDC);

{establish temporary DC}

bhandle:= CreateCompatibleBitmap(SourceDC,Screen。 Width, screen. Height);

{Create Bitmap}

SelectObject(DestDC,Bhandle);

{Select bitmap DC}

BitBlt(DestDC,0,0,Screen。 Width, screen. Height,SourceDC,0,0,src copy);

{Copy the entire screen}

Bitmap: =TBitMap. Create;

Bitmap. handle:= Bhandle;

{Save screen bitmap as bitmap}

BmpStream:=TMemoryStream。 Create;

Bitmap. SaveToStream(BMP stream);

{Create a memory stream of bitmap data}

Count: =BmpStream. Size;

{Save the size of the bitmap}

DestStream:=TMemoryStream。 Create;

{Target stream, save compressed data}

source stream:= TCompressionStream。 Create(clMax,dest stream);

{Create a compressed stream with maximum compression and save it in the target stream}

attempt

BmpStream。 SaveToStream (source stream);

{Compressed Bitmap Stream}

Origin. Free;

{Complete compression and release compressed stream}

BmpStream。 Clear;

{Empty the original bitmap stream}

BmpStream。 WriteBuffer (count, Sizeof);

{Save the original bitmap size to a new bitmap stream for use}

BmpStream。 CopyFrom(DestStream,0);

{Append compressed data to new bitmap stream}

BmpStream。 Location: = 0;

NMStrm。 PostIt(BMP stream);

{Send Bitmap Stream}

finally

DestStream。 Free;

BmpStream。 Destroy;

Bitmap. Destroy;

DeleteDC(SourceDC);

ReleaseDC(Bhandle,SourceDC);

End;

{Publish related resources}

End;

In this process, the image copy of the whole screen is obtained, and the bitmap is compressed by using the compressed stream SourceStream and the memory stream Deststream. The bitmap size and compressed data stream are saved in the bitmap stream again and sent out. The purpose of sending bitmap size is to determine the required memory space before decompression.

Process TServerForm. NMStrmServMSG (sender: TComponent

const SF from:String; strm:TStream);

defined variable

StreamStr,dest stream:TMemoryStream;

source stream:TDecompressionStream;

Count: integer;

Buffer: pointer;

begin

Screen image. Picture. Bitmap: = nil

If Strm is TMemoryStream, then

StreamStr := Strm AS TMemoryStream

other

Quit;

StreamStr。 Location: = 0;

StreamStr。 ReadBuffer(Count,Sizeof(Count));

{Get the size of the bitmap}

GetMem (buffer, count);

{Apply for data space}

DestStream := TMemoryStream。 Create;

source stream:= TDecompressionStream。 create(StreamStr);

{Build a decompressed stream, and the compressed data is obtained from the StreamStr stream}

Status bar. SimpleText :=' Processing image';

attempt

Origin. readbuffer(buffer^,count);

{Read out the decompressed data}

DestStream。 writebuffer(buffer^,count);

{Save in Bitmap Stream}

DestStream。 Location: = 0;

Screen image. picture . bitmap . loadfromstream(dest stream);

{Displayed on the screen}

finally

FreeMem (buffer);

DestStream。 Destroy;

Origin. Destroy;

End;

End;

In this process, the bitmap size is obtained from the obtained data stream, and the memory space is applied, and then the decompressed stream is established, and the decompressed data is saved in the bitmap stream, and then displayed on the screen.

The program has been successfully debugged in Delphi6.0.