Delphi Data Compression Processing (2)

zhaozj2021-02-17  55

Delphi Data Compression Processing (2)

Buffer: pchar;

Count: integer;

Begin

File: // Read the size of the original image from the compressed image stream

CompressedStream.Readbuffer (Count, Sizeof (count);

File: / / According to the size of the image size, allocate memory blocks to be read.

GetMem (buffer, count);

Deststream: = TMEMORYSTREAM.CREATE;

SourceStream: = TDecompressionStream.create (CompressedStream);

Try

File: // Reconnect the compressed image, and then store into the buffer memory block

SourceStream.Readbuffer (buffer ^, count);

File: // Save the original image stream to the DestStream stream

Deststream.writebuffer (buffer ^, count);

DestStream.position: = 0; // Reset stream pointer

// Load the original image stream from the DestStream stream

Bmp.LoadFromstream (Deststream);

Finally

FreeMem (buffer);

Deststream.free;

END;

END;

4. Compressed button onclick event

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

VAR

BMP: TBITMAP;

CompressedStream: TMemoryStream;

Begin

BMP: = Tbitmap.create;

CompressedStream: = TMEMORYSTREAM.CREATE

Try

File: // Capture the current screen, save the image to GetScreen (BMP) in the BMP object;

File: // Save the image in the BMP object to the memory stream

Bmp.savetostream (CompressedStream);

File: // Compress the original image stream according to the default compression ratio

CompressBitmap (CompressedStream, CLDEFAULT);

File: // Save the image stream after the compression as a file in custom format

CompressedStream.savetofile ('c: /cj.dat');

Finally

Bmp.free;

CompressedStream.Free;

END;

END;

5. Unzip button onclick event

Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT);

VAR

CompressedStream: TfileStream;

BMP: TBITMAP;

Begin

BMP: = Tbitmap.create;

File: // Open a custom compressed format file with a read-only mode of the file stream

CompressedStream: = TFileStream.create ('c: /cj.dat', fmopenread);

Try

File: // Decompress the compressed image stream

UncompressBitmap (CompressedStream, BMP);

File: / / Restore the original image stream into the specified BMP file

Bmp.savetofile ('c: /cj.bmp');

Finally

Bmp.free;

CompressedStream.Free;

END;

END;

In addition, the TcompressionStream object also provides the CompressionRate property, which is used to describe the compression ratio after compression of the original data, and the onprogress event is triggered during the compression and decompression process, and the developer can write the progress in the event. Code.

转载请注明原文地址:https://www.9cbs.com/read-30758.html

New Post(0)