Sender: Stony (My Digital Stony), the reason for Shanghai), the letter area: Borland Title: Delphi Data Compression Processing [ZZ] Send Station: Drinking Water Source (11:05 Wednesday, 2004), letter
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. Resolving the idea 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. Specifically achieve new project files, in the interface part of the main unit reference Zlib.PAS, place two buttons button1, button2 on the main form, write the corresponding process call code in their OnClick event.
Part of the 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} end; var form1: tform1; importation {$ r * .dfm} 1. Capture full screen image Procedure GetScreen (var); 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 adjust 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. Image compression procedure CompressBitmap (var CompressedStream: TMemoryStream; const CompressionLevel: TCompressionLevel); var SourceStream: TCompressionStream; DestStream: TMemoryStream; Count: Integer; Begin // get the original size of the image stream Count: = CompressedStream.Size; DestStream: = TMemoryStream. Create; SourceStream: = TCompressionStream.Create (CompressionLevel, DestStream); Try // SourceStream stored in the original image stream CompressedStream.SaveToStream (SourceStream); // original image stream is compressed, DestStream image stored in the compressed stream SourceStream.Free; CompressedStream.Clear; // write the original image size CompressedStream.WriteBuffer (Count, SizeOf (Count)); // write the compressed image stream CompressedStream.CopyFrom (DestStream, 0); finally DestStream.Free ;
END; END; 3. Compressed image reduction procedure UnCompressBitmap (const CompressedStream: TFileStream; var Bmp: TBitmap); var SourceStream: TDecompressionStream; DestStream: TMemoryStream; Buffer: PChar; Count: Integer; Begin // read compressed image from the original image stream size CompressedStream.ReadBuffer (Count, SizeOf (Count)); // the image size of the original to be read into the image stream allocated block GetMem (Buffer, Count); DestStream: = TMemoryStream.Create; SourceStream: = TDecompressionStream.Create (CompressedStream); try // is compressed by compressed image, and then store SourceStream.readbuffer (buffer ^, count) in the buffer memory block; // Save the original image stream to DestStream.WriteBuffer (buffer ^ COUNT); DestStream.position: = 0; // Reset stream pointer // From the DestStream stream into the original image stream Bmp.LoadFromstream (DestStream); factory freemem (buffer); de; End; 4. Compression button onClick event procedure TForm1.Button1Click (Sender: TObject); var Bmp: TBitmap; CompressedStream: TMemoryStream; begin Bmp: = TBitmap.Create; CompressedStream: = TMemoryStream.Create; Try // current capture the entire screen, save the images to GetScreen (BMP) in the BMP object; // saves images in the BMP object to bmp.savetostream (compressedstream) in the memory stream; // compresses compressbitMap (CompressedStream, CLDEFAULT) by default compression ratio. / Save the image stream after compression as a file compressedstream.savetofile ('c: /cj.dat'); Finally Bmp.free; compressedStream.free; end; end; 5. Decompression button onClick event procedure TForm1.Button2Click (Sender: TObject); var CompressedStream: TFileStream; Bmp: TBitmap; begin Bmp: = TBitmap.Create; // read-only stream file compression format file open custom CompressedStream: = TFileStream.create ('c: /cj.dat', fmopenread; try // to decompress the compressed image stream to uncompressbitMap (CompressedStream, BMP); // Restore the original image stream to the specified BMP file BMP. SaveTofile ('c: /cj.bmp'); Finally Bmp.free;