码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi简单生成图片验证码

时间:2019-08-22 18:53:38      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:img   ima   pos   nap   src   utils   画线   line   form   

技术图片
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.
View Code

 

Delphi简单生成图片验证码

标签:img   ima   pos   nap   src   utils   画线   line   form   

原文地址:https://www.cnblogs.com/Rootloading/p/11395813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!