Delphi data compression processing(1)
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.
Solution ideas
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.
Specific implementation
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:
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 true color and can also be adjusted according to actual needs
Bmp.PixelFormat := pf24bit;
Bmp.Width := MyRect.Right;
Bmp.Height := MyRect.Bottom;
file://capture whole screen image
Bmp.Canvas.CopyRect(MyRect, MyCanvas, MyRect);
finally
MyCanvas.Handle := 0;
MyCanvas.Free;
ReleaseDC(0, Dc);
end;
end;
2. Compress images
procedure CompressBitmap(var CompressedStream: TMemoryStream;const CompressionLevel: TCompressionLevel);
var
SourceStream: TCompressionStream;
DestStream: TMemoryStream;
Count: Integer;
Begin
file://gets the original size of the image stream
Count := CompressedStream.Size;
DestStream := TMemoryStream.Create;
SourceStream:=TCompressionStream.Create
(CompressionLevel, DestStream);
Try
The original image stream is stored in file://SourceStream
CompressedStream.SaveToStream(SourceStream);
file://compresses the original image stream, and the compressed image stream is stored in DestStream
SourceStream.Free;
CompressedStream.Clear;
file://writes the dimensions of the original image
CompressedStream.WriteBuffer(Count, SizeOf
(Count));
file://Write a compressed image stream
CompressedStream.CopyFrom(DestStream, 0);
finally
DestStream.Free;
end;
end;
3. Restore compressed images
procedure UnCompressBitmap(const CompressedStream: TFileStream; var Bmp: TBitmap);
var
SourceStream: TDecompressionStream;
DestStream: TMemoryStream;