Delphi简单生成图片验证码
Posted rootloading
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi简单生成图片验证码相关的知识,希望对你有一定的参考价值。
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,captcha_code, Vcl.ExtCtrls,Vcl.Imaging.jpeg,Vcl.Imaging.pngimage, Vcl.StdCtrls,System.UIConsts,System.Math,Soap.EncdDecd; const // CodeChar: array [0 .. 34] of char = ( // ‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘, // ‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,‘H‘,‘I‘, // ‘J‘,‘K‘,‘L‘,‘M‘,‘N‘,‘O‘,‘P‘,‘Q‘,‘R‘, // ‘S‘,‘T‘,‘U‘,‘V‘,‘W‘,‘X‘,‘Y‘,‘Z‘); CodeChar: array [0 .. 34] of char = ( ‘我‘,‘的‘,‘爱‘,‘他‘,‘他‘,‘随‘,‘你‘,‘吗‘,‘中‘, ‘过‘,‘人‘,‘圈‘,‘全‘,‘菜‘,‘渔‘,‘后‘,‘墙‘,‘要‘, ‘香‘,‘港‘,‘澳‘,‘情‘,‘感‘,‘值‘,‘得‘,‘宾‘,‘开‘, ‘枪‘,‘飞‘,‘鸡‘,‘鸭‘,‘懂‘,‘动‘,‘车‘,‘无‘); type TForm1 = class(TForm) Memo1: TMemo; Panel1: TPanel; Button2: TButton; Image1: TImage; Label1: TLabel; procedure Button2Click(Sender: TObject); private Private declarations public Public declarations end; var Form1: TForm1; implementation $R *.dfm procedure TForm1.Button2Click(Sender: TObject); var I,H:Integer; img:TBitmap; x,y:Integer; Rstr,tmp:RawByteString; png:TPngImage; Base64In:TMemoryStream; Base64Out:TStringStream; BaseStr:AnsiString; begin try img:=TBitmap.Create; img.Width:=280; img.Height:=100; img.Canvas.Pen.Width:=1; img.Canvas.Font.Size:=30; img.Canvas.Font.Name:=‘微软雅黑‘; img.Canvas.Font.Color:=clGreen; //随机画线400条 for I := 0 to 400 do begin Randomize; Img.Canvas.Pen.Color:=MakeColor(Random(256) and $C0, 200); Img.Canvas.MoveTo(Random(100),Random(280)); Img.Canvas.LineTo(Random(280),Random(100)); end; img.Canvas.Font.Color:=clBlack; //img.Canvas.Font.Style:=[TFontStyle.fsBold]; x:=0; y:=0; //最多有四个字符 for H :=0 to 3 do begin Randomize; Rstr:=CodeChar[Random(34)]; tmp:=tmp+rstr; img.Canvas.Brush.Style:=bsClear; img.Canvas.TextOut(RandomRange(y,y+60),Random(50),Rstr); y:=y+60; x:=x+10; end; png:=TPngImage.Create; png.Assign(img); Label1.Caption:=tmp; Base64In:=TMemoryStream.Create; Base64Out:=TStringStream.Create; png.SaveToStream(Base64In); Base64In.Position:=0; //将图片流数据编码为base64 EncodeStream(Base64In,Base64Out); Base64Out.Position:=0; Memo1.Lines.Clear; Memo1.Lines.Add(Base64Out.DataString); Image1.Picture.Bitmap.Assign(img); finally Base64In.Free; Base64Out.Free; png.Free; img.Free; end; end; end.
以上是关于Delphi简单生成图片验证码的主要内容,如果未能解决你的问题,请参考以下文章
验证码识别与生成类API调用的代码示例合集:六位图片验证码生成四位图片验证码生成简单验证码识别等