Delphi Data Compression Processing (1)
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);
File: // The image is 24-bit color, or it can be adjusted according to actual needs.
Bmp.pixelformat: = pf24bit;
Bmp.width: = myRect.right;
Bmp.height: = myRect.bottom;
File: // 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;
Count: integer;
Begin
File: // Get the original size of the image stream
Count: = compressedStream.size;
Deststream: = TMEMORYSTREAM.CREATE;
SourceStream: = TcompressionStream.create
CompressionLevel, DeststReam;
Try
File: // SourceStream saves the original image stream
CompressedStream.saveTostream (SourceStream);
File: // Compress the original image stream, and the compressed image stream is saved in DestStream
SourceStream.Free;
CompressedStream.clear;
File: // Write the size of the original image
CompressedStream.writebuffer (Count, Sizeof
COUNT);
File: // Write the 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;