Delphi 生成随机验证码
function CodeImg(img: Timage): string;
var
I,j,k: Integer;
vPoint: TPoint;
vLeft: Integer;
arrStr:array [1..36]of string ;
strResult: string;
begin
strResult := '';
arrStr[1] := '0'; arrStr[2]:='1'; arrStr[3]:='2'; arrStr[4]:='3';
arrStr[5] := '4'; arrStr[6]:='5'; arrStr[7]:='6'; arrStr[8]:='7';
arrStr[9] := '8'; arrStr[10]:='9'; arrStr[11]:='A'; arrStr[12]:='B';
arrStr[13] := 'C'; arrStr[14]:='D'; arrStr[15]:='E'; arrStr[16]:='F';
arrStr[17] := 'G'; arrStr[18]:='H'; arrStr[19]:='I'; arrStr[20]:='J';
arrStr[21] := 'K'; arrStr[22]:='L'; arrStr[23]:='M'; arrStr[24]:='N';
arrStr[25] := 'O'; arrStr[26]:='P'; arrStr[27]:='Q'; arrStr[28]:='R';
arrStr[29] := 'S'; arrStr[30]:='T'; arrStr[31]:='U'; arrStr[32]:='V';
arrStr[33] := 'W'; arrStr[34]:='X'; arrStr[35]:='Y'; arrStr[36]:='Z';
For j:=1 to 4 do
begin
Randomize;
k := strtoint(Format('%.1d',[Random(36)]));
strResult := strResult + trim(arrStr[k])
end;
vLeft := 10;
img.picture := nil;
for I := 1 to Length(strResult) do
begin
with Img do
begin
Canvas.Font.Size := Random(10)+ 9;
Canvas.Font.Color := RGB(Random(256) and $C0,
Random(256) and $C0, Random(256) and $C0);
if Random(2)=1 then
Canvas.Font.Style := [fsBold]
else Canvas.Font.Style := [];
begin
Canvas.Font.Name := Screen.Fonts[10];
vPoint.X := Random(4)+ vLeft;
vPoint.Y := Random(5);
Canvas.TextOut(vPoint.X, vPoint.Y,strResult[I]);
vLeft := vPoint.X + Canvas.TextWidth(strResult[I]);
end;
end;
end;
end;