The RAD development tool Delphi 5.0 launched by Borland Company is a mainstream development tool on the Windows platform. Its visual development environment and powerful object-oriented programming functions have attracted countless developers. However, some programmers often struggle with compressing large amounts of data during the actual development process, and have to find some efficient compression algorithms or search for third-party controls on the Internet to achieve compression. Doesn't Delphi itself provide this function? In fact, Delphi programmers have already considered this. They provided two unit files, Zlib.pas and Zlibconst.pas, to solve the data compression problem and achieve a high data compression ratio. These two files are saved in the InfoExtras lib directory on the Delphi 5.0 installation CD. In addition, the Obj file referenced by the Zlib.pas unit is also saved in the InfoExtras libObj directory. The following article takes compressing a screen copy as an example to introduce how to use this function.
First, use screen copy to capture the current image of the entire screen, and then save it in the memory as a BMP file format. When compressing, use the TComPRessionStream object to compress the original image and save it in a custom file format; when decompressing, use the TDecompressionStream object to decompress the compressed image and restore it to a BMP format image file. |
Create a new project file, reference Zlib.pas in the interface part of the main unit, place two buttons Button1 and Button2 on the main form, and write the corresponding procedure call code in their OnClick event. |
Part of the program source code is as follows: |
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Zlib; |
procedure Button1Click(Sender: TObject); |
procedure Button2Click(Sender: TObject); |
1. Capture full screen image |
procedure GetScreen(var Bmp: TBitmap); |
MyCanvas := TCanvas.Create; |
MyRect:=Rect(0, 0,Screen.Width, Screen.Height); |
//The image is 24-bit true color, which can also 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); |
procedure CompressBitmap(var CompressedStream: TMemoryStream;const CompressionLevel: TCompressionLevel); |
SourceStream: TCompressionStream; |
DestStream: TMemoryStream; |
//Get the original size of the image stream |
Count := CompressedStream.Size; |
DestStream := TMemoryStream.Create; |
SourceStream:=TCompressionStream.Create |
(CompressionLevel, DestStream); |
//SourceStream stores the original image stream |
CompressedStream.SaveToStream(SourceStream); |
//Compress the original image stream, and the compressed image stream is stored in DestStream. |
//Write the size of the original image |
CompressedStream.WriteBuffer(Count, SizeOf |
//Write the compressed image stream |
CompressedStream.CopyFrom(DestStream, 0); |
3. Restore compressed images |
procedure UnCompressBitmap(const CompressedStream: TFileStream; var Bmp: TBitmap); |
SourceStream: TDecompressionStream; |
DestStream: TMemoryStream; |
//Read the size of the original image from the compressed image stream |
CompressedStream.ReadBuffer(Count, SizeOf(Count)); |
//Allocate memory blocks for the original image stream to be read based on the image size |
DestStream := TMemoryStream.Create; |
SourceStream := TDecompressionStream.Create(CompressedStream); |
//Decompress the compressed image stream and store it 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); |
4. Compression button OnClick event |
procedure TForm1.Button1Click(Sender: TObject); |
CompressedStream: TMemoryStream; |
CompressedStream := TMemoryStream.Create; |
//Capture the entire current screen and save the image to the Bmp object GetScreen(Bmp); |
//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 compressed image stream into a custom format file |
CompressedStream.SaveToFile(' C:cj.dat' ); |
5. Unzip button OnClick event |
procedure TForm1.Button2Click(Sender: TObject); |
CompressedStream: TFileStream; |
//Open a custom compressed format file in read-only mode of the file stream |
CompressedStream := TFileStream.Create(' C:cj.dat' , fmOpenRead); |
//Decompress the compressed image stream |
UnCompressBitmap(CompressedStream, Bmp); |
//Restore the original image stream to the specified BMP file |
Bmp.SaveToFile(' C:cj.bmp' ); |
In addition, the TCompressionStream object also provides the CompressionRate property, which is used to describe the compression ratio after compressing the original data. The OnProgress event is triggered during the compression and decompression processes. Developers can write in this event to display the progress. code. |
The above code passes debugging and running in Delphi 5.0. |