Examples Delphi

Every homepage got to have one! Let's make a hitcounter.
This hitcounter produces an image, which is built with seven individual number segments, pasted into a frame image. The maximum count would be 10 million hits, enough for most homepages, mine included !
The number images are 11 x 20 pixels, contained in an ImageList component like this:

The frame image is 81 x 24 pixels, and looks like this:
We'll use a textfile for storing the hit count. Each time the page is accessed, we increment this value, find the corresponding image from the imagelist for each value position and paste each image into the frame at right positions. When ready, we convert the bitmap image to a jpg, and save it to a TMemorystream, and finally we send the stream as response to the user.
The hitcounter is called from the HTML code just like any other image would be:
XXX is the name of the hitcount file without extension.
If you want the project files, press here:
An admin exe is included.
{-----------------------------------------------------------------------------
Unit Name: Unit1
Author: Mats Asplund
Purpose: A web hitcounter
Usage: 1. Create a textfile and write the startcount on the first row.
Name it: XXX.cnt
2. Place frame.bmp in the /cgi-bin library.
3. Place a link: on the webpage.
-----------------------------------------------------------------------------}
unit Unit1;
interface
uses
SysUtils, Classes, HTTPApp, ImgList, Controls, Graphics, JPeg;
type
TWebModule1 = class(TWebModule)
ImageList1: TImageList;
procedure WebModule1WebActionItem1Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
end;
var
WebModule1: TWebModule1;
implementation
{$R *.DFM}
procedure TWebModule1.WebModule1WebActionItem1Action(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
Jpg: TJpegImage;
Number, Frame: TBitMap;
CountStr, CounterName: string;
HitCount, n: integer;
Count: TStringList;
ResponseStream: TMemoryStream;
begin
ResponseStream:= TMemoryStream.Create;
Number:= TBitMap.Create;
Frame:= TBitMap.Create;
Jpg := TJpegImage.Create;
Count:= TStringList.Create;
try
// Get name of the counter from request and load the corresponding file
CounterName:= Request.QueryFields[0];
Count.LoadFromFile(CounterName + '.cnt');
// Increment hitcount and save it back to file
HitCount:= StrToInt(Count[0]);
Inc(HitCount);
Count[0]:= IntToStr(HitCount);
CountStr:= Count[0];
Count.SaveToFile(CounterName + '.cnt');
// Add leading zeros to count
case Length(CountStr) of
1: CountStr:= '000000' + CountStr;
2: CountStr:= '00000' + CountStr;
3: CountStr:= '0000' + CountStr;
4: CountStr:= '000' + CountStr;
5: CountStr:= '00' + CountStr;
6: CountStr:= '0' + CountStr;
end;
// Get all seven counternumbers from ImageList and paste them into the
// the frame bitmap at right positions.
Frame.LoadFromFile('frame.bmp');
for n:= 1 to 7 do
begin
// Number bitmaps are 11 x 20 pix. Frame bitmap is 81 x 24 pix.
ImageList1.GetBitmap(StrToInt(CountStr[n]), Number);
Frame.Canvas.Draw(2 + (11 * (n - 1)), 2, Number);
end;
// Convert the resulting bitmap to jpg and save it to the stream
Jpg.Assign(Frame);
Jpg.SaveToStream(ResponseStream);
// Send the stream as response
ResponseStream.Position:= 0;
Response.ContentType := 'image/jpeg';
Response.ContentStream:= ResponseStream;
Response.SendResponse;
finally
Number.Free;
Frame.Free;
Count.Free;
end;
end;
end.