问题来源:http://www.cnblogs.com/del/archive/2010/04/24/1719631.html#2079634

使用下面的 Hex2Png() 函数解析黑人给的数据,结果是一个 93396 的验证码图片。

uses pngimage;

{从 png 图片到十六进制字符串}
function Png2Hex(png: TPngImage): string;
var
  stream: TMemoryStream;
begin
  stream := TMemoryStream.Create;
  png.SaveToStream(stream);
  SetLength(Result, stream.Size * 2);
  BinToHex(stream.Memory, PChar(Result), stream.Size);
  stream.Free;
end;

{从十六进制字符串恢复 png 图片}
procedure Hex2Png(str: string; out png: TPngImage);
var
  stream: TMemoryStream;
begin
  if not Assigned(png) then png := TPngImage.Create;
  stream := TMemoryStream.Create;
  stream.SetSize(Length(str) div 2);
  HexToBin(PChar(str), stream.Memory, stream.Size);
  png.LoadFromStream(stream);
  stream.Free;
end;

{测试}
procedure TForm1.Button1Click(Sender: TObject);
var
  png: TPngImage;
begin
  png := TPngImage.Create;
  png.LoadFromFile('c:\temp\test.png');
  Memo1.Text := Png2Hex(png);
  png.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  png: TPngImage;
begin
  png := TPngImage.Create;
  Hex2Png(Memo1.Text, png);
  Canvas.Draw(0, 0, png);
  png.Free;
end;

posted on 2011-04-26 22:53  万一  阅读(4634)  评论(4编辑  收藏  举报