我写过一个 ISAPI 计数器,可能对你有帮助:
我的思路是:
先用TBitmap生成图形,
然后转存到TJPEGImage,
然后写到TMemoryStream,
最后用Response.SendStream()输出。
请看源代码:
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, HTTPApp, Graphics, JPEG,
ImgList, Controls;
type
TWebCounter = class(TWebModule)
CountList: TImageList;
procedure WebCounteraCounterAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
WebCounter: TWebCounter;
implementation
{$R *.DFM}
procedure TWebCounter.WebCounteraCounterAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
FileName,Count:string;
CF:TextFile;
MS:TMemoryStream;
Bitmap:TBitmap;
i:integer;
begin
//读取并增加计数器的数字
FileName:=Request.QueryFields.Values['filename'];
FileName:='d:/inetpub/wwwroot/counter/data/'+FileName+'.txt';
if FileExists(FileName) then
begin
AssignFile(CF,FileName);
Reset(CF);
ReadLn(CF,Count);
Count:=IntToStr(StrToInt(Count)+1);
while Length(Count)<6 do
begin
Count:='0'+Count;
end;
Rewrite(CF);
WriteLn(CF,Count);
CloseFile(CF);
end;
//把字符转换为图形
Bitmap:=TBitmap.Create;
with Bitmap do
begin
Width:=72;
Height:=18;
for i:=0 to 5 do
begin
CountList.Draw(Bitmap.Canvas,i*11+3,3,StrToInt(Count[i+1]),true)
end;
end;
MS:=TMemoryStream.Create;
//转换为THPEGimage格式
with TJPEGImage.Create do
begin
Assign(Bitmap); //从TBitmap中复制
SaveToStream(MS); //转存到“内存流”
Free;
end;
MS.Position:=0; //这几句必不可少!!
Response.ContentType:='image/jpg'; //设置返回格式为:“image/jpg”
Response.SendResponse; //
Response.SendStream(MS); //从内存流中发送
MS.Free;
Bitmap.Free;
end;
end.
调用方法:
<p>您是第<img src="/scripts/counter.dll?webname=test">位访问者</p>
很容易就可把它改成CGI