Hier sind Beispiele, wie man Bilder in einen HEX oder auch zurückberechnen kann.
Kompilierbar nur für höhere Delphi-Versionen.
uses System.SysUtils, PngImage, Math
type
TByteArray = array [0..32767] of Byte;
//
procedure Hex2Png(str: string; out png: TPngObject);
var stream: TMemoryStream;
begin
if not Assigned(png) then png := TPngObject.Create;
stream := TMemoryStream.Create;
stream.SetSize(Length(str) div 2);
HexToBin(PChar(str), stream.Memory, stream.Size);
png.LoadFromStream(stream);
stream.Free;
end;
function Png2Hex(png: TPngObject): 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;
function bmp2Hex(out bmp: TBitmap):string;
var stream: TMemoryStream;
begin
stream := TMemoryStream.Create;
bmp.SaveToStream(stream);
SetLength(Result, stream.Size * 2);
BinToHex(stream.Memory, PChar(Result), stream.Size);
stream.Free;
end;
procedure Hex2bmp(str: string; out bmp: TBitmap);
var stream: TMemoryStream;
begin
if not Assigned(bmp) then bmp := TBitmap.Create;
stream := TMemoryStream.Create;
stream.SetSize(Length(str) div 2);
HexToBin(PChar(str), stream.Memory, stream.Size);
bmp.LoadFromStream(stream);
stream.Free;
end;
function HexToInt(Hexa: string): LongWord;
const
ValoresHexa: array['A'..'F'] of integer = (10, 11, 12, 13, 14, 15);
var
nDecimal: LongWord;
nIndex: byte;
begin
nDecimal := 0;
Hexa := Uppercase(Hexa);
for nIndex := Length(Hexa) downto 1 do
if Hexa[nIndex] in ['0'..'9']
then nDecimal := nDecimal + StrToInt(Hexa[nIndex]) *
Trunc(Exp((Length(Hexa) - nIndex) * ln(16)))
else nDecimal := nDecimal + ValoresHexa[Hexa[nIndex]] *
Trunc(Exp((Length(Hexa) - nIndex) * ln(16)));
Result := nDecimal;
end;
function Hex2Byte2(sRandomPwd:String):TByteArr;
var
Buf:TByteArr;
i:Integer;
begin
sRandomPwd := StringReplace(sRandomPwd,' ', '',[]);
if ((Length(sRandomPwd) mod 2) <> 0) then
begin
sRandomPwd := sRandomPwd+' ';
end;
i:= Trunc(Length(sRandomPwd)/2);
SetLength(Buf,i);
for i := 0 to Length(Buf)-1 do
begin
Buf[i]:=HexToInt(Copy(sRandomPwd,i*2+1,2));
end;
Result:=Buf;
end;
function Hex2Byte(sRandomPwd:String):TByteArr;
var
Buf:TByteArr;
begin
SetLength(Buf, Length(sRandomPwd) div 2);
// HexToBin(PAnsiChar(sRandomPwd), @Buf[0], Length(sRandomPwd) div 2);
//HexToBin(PChar(sRandomPwd), @Buf[0], Length(sRandomPwd) div 2);
HexToBin(PAnsiChar(sRandomPwd), @Buf[0], Length(sRandomPwd) div 2);
Result:=buf;
end;
function Byte2Hex(arrByte:TByteArr):string;
var stream: TMemoryStream;
begin
stream := TMemoryStream.Create;
stream.Write(arrByte[0],Length(arrbyte));
SetLength(Result, stream.Size * 2);
BinToHex(stream.Memory, PChar(Result), stream.Size);
stream.Free;
end;
function Byte2Hex2(arrByte:TByteArr):string;
var
i:integer;
begin
for i:=0 to length(arrByte)-1 do
begin
Result := Result + Copy( IntToHex(arrByte[i],16),15,2)
end;
end;
Beispiele :
procedure TForm1.btnHex2Png(Sender: TObject);
var png: TPngObject;
begin
png := TPngObject.Create;
Hex2Png(Memo1.Text, png);
Canvas.Draw(0, 0, png);
png.Free;
end;
procedure TForm1.btnPng2HexClick(Sender: TObject);
var png: TPngObject;
begin
png := TPngObject.Create;
png.LoadFromFile('dyc.png');
Memo1.Text := Png2Hex(png);
png.Free;
end;
procedure TForm1.btnBmp2HexClick(Sender: TObject);
var bmp: TBitmap;
begin
image1.Picture.Bitmap.SaveToFile('2.bmp');
bmp := TBitmap.Create;
bmp.LoadFromFile('2.bmp');
Memo1.Text := bmp2Hex(bmp);
bmp.Free;
end ;
procedure TForm1.btnHex2BmpClick(Sender: TObject);
var bmp: tbitmap;
begin
bmp := tbitmap.Create;
Memo1.Text:=StringReplace(Memo1.Text,#13,'',[]);
Memo1.Text:=StringReplace(Memo1.Text,#10,'',[]);
Hex2bmp(Memo1.Lines.GetText,bmp);
Canvas.Draw(1, 1, bmp);
bmp.Free;
end;
procedure TForm1.btnByte2HexClick(Sender: TObject);
var
m:TMemoryStream;
b:TByteArr;
begin
m:=TMemoryStream.Create();
Image1.Picture.Bitmap.SaveToStream(m);
m.Position:=0;
SetLength(b,m.size);
m.ReadBuffer(b[0],m.Size);
Memo1.Text:=Byte2Hex2(b);
end;
procedure TForm1.btnHex2ByteClick(Sender: TObject);
var
m:TMemoryStream;
b:TByteArr;
bmp:TBitmap;
begin
b:=Hex2Byte2(Memo1.text);
m:=TMemoryStream.Create;
m.WriteBuffer(b[0],Length(b));
m.Position:=0;
m.Seek(0,soBeginning);
bmp:=TBitmap.Create;
bmp.LoadFromStream(m);
Canvas.Draw(0,0,bmp);
end;
Keine Kommentare:
Kommentar veröffentlichen