unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
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:\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;
end.
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 254
ClientWidth = 480
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 64
Top = 208
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object Button2: TButton
Left = 200
Top = 208
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 2
OnClick = Button2Click
end
object Memo1: TMemo
Left = 8
Top = 8
Width = 449
Height = 177
Lines.Strings = (
'Memo1')
TabOrder = 0
end
end