Delphi's compression flow and decompression flow

zhaozj2021-02-11  235

Originally published in Computer World Daily:

http://www.ccw.com.cn/htm/app/aprog/01_1_12_4.asp

Delphi data compression processing

Cai Jian

Mailto: chaijian@21cn.com

Borland's RAD Development Tools Delphi 5.0 serves as a mainstream development tool on a Windows platform, its visualized development environment and object-oriented programming have attracted countless developers. However, some programmers often compress a large amount of data while the actual development process is often compressed, and has to find some highly efficient compression algorithms or to find third-party controls on the Internet to implement compression. Does Delphi do not provide this feature? In fact, Delphi's programming designer has long been considering this, providing Zlib.Pas and ZlibConst.Pas two unit files to solve data compression issues, achieving a high data compression ratio. These two files are saved in the Delphi 5.0 installation disc / info / extras / zlib directory, in addition, the OBJ file referenced by the Zlib.PAS unit is saved in the INFO / Extras / Zlib / ObJ directory. Below will be described herein as an example to describe how this feature is used as an example.

Solutions

First capture the image of the current entire screen with a screen copy, then save it as a BMP file format in memory. When compressed, the original image is compressed and saved as a custom file format using the TcompressionStream object; decompressing the compressed image, reducing the image file of the BMP format using the TDeCompressionStream object.

Implementation

Create a new project file, reference Zlib.PAS in the interface part of the main unit, and place two buttons button1, button2 on the main form, write the corresponding procedure call code in their OnClick event.

Some program source code is as follows:

Unit unit1;

Interface

Uses

Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Zlib

Type

TFORM1 = Class (TFORM)

Button1: tbutton;

Button2: tbutton;

Procedure Button1Click (Sender: TOBJECT);

Procedure Button2Click (Sender: TOBJECT);

Private

{Private Declarations}

public

{Public declarations}

END;

VAR

FORM1: TFORM1;

IMPLEMentation

{$ R * .dfm}

1. Capture full screen image

Procedure getScreen (var bmp: tbitmap);

VAR

DC: HDC;

Mycanvas: tcanvas;

MYRECT: TRECT;

Begin

DC: = getwindowdc (0);

Mycanvas: = tcanvas.create;

Try

Mycanvas.handle: = DC;

MyRect: = Rect (0, 0, Screen.width, Screen.Height);

// The image is 24-bit true color, or it can be adjusted according to actual needs.

Bmp.pixelformat: = pf24bit;

Bmp.width: = myRect.right;

Bmp.height: = myRect.bottom;

// Capture the entire screen image

Bmp.canvas.copyRect (MyRect, Mycanvas, MyRect); Finally

Mycanvas.handle: = 0;

Mycanvas.free;

ReleaseDC (0, DC);

END;

END;

2. Compressed image

Procedure CompressBitmap (Var CompressedStream; Const CompressionLevel: TcompressionLevel);

VAR

SourceStream: TcompressionStream;

Deststream: TMemoryStream DESTSTREAM

Count: integer;

Begin

// Get the original size of the image stream

Count: = compressedStream.size;

Deststream: = TMEMORYSTREAM.CREATE;

SourceStream: = TcompressionStream.create

CompressionLevel, DeststReam;

Try

The original image stream is saved in // SourceStream

CompressedStream.saveTostream (SourceStream);

/ / Compress the original image stream, and the compressed image stream is saved in DestStream

SourceStream.Free;

CompressedStream.clear;

// Write the size of the original image

CompressedStream.writebuffer (Count, Sizeof

COUNT);

// Write a compressed image stream

CompressedStream.copyFrom (Deststream, 0);

Finally

Deststream.free;

END;

END;

3. Restore is compressed image

Procedure UncompressBitmap (Const CompressedStream; var bmp: tbitmap);

VAR

SourceStream: TDECompRessionsTream;

Deststream: TMemoryStream DESTSTREAM

Buffer: pchar;

Count: integer;

Begin

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

CompressedStream.Readbuffer (Count, Sizeof (count);

/ / 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

/ / The compressed image is compressed and then stored in the buffer memory block.

SourceStream.Readbuffer (buffer ^, count);

// 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

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

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

Bmp.savetostream (CompressedStream);

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

CompressBitmap (CompressedStream, CLDEFAULT);

// Save the image stream after the compression as a file in a 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;

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

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

Try

// Unzip the compressed image stream

UncompressBitmap (CompressedStream, BMP);

// 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.

The above code is transferred in Delphi 5.0.

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

New Post(0)