首页 > 代码库 > 太有用了,所以转:Delphi下16进制位图数据转位图

太有用了,所以转:Delphi下16进制位图数据转位图

如果我们在Form中拖入一个Image控件,并设置好picture后,Alt+F12就可以看到Form的源代码中已经将图片转成了16进制字符串,如下:

object Image1: TImage    Left = 536    Top = 24    Width = 105    Height = 105    Picture.Data = {      07544269746D61703E040000424D3E0400000000000036000000280000000700      00002B000000010018000000000008040000C40E0000C40E0000000000000000      0001BBE7F4BCE8F5BDE9F6BBE7F4BDE9F6BFEBF8BEEAF7000000B0E9F2B0E9F2      B0E9F2B0E9F2B1EAF3B2EBF4B1EAF3000000ADE5F8ADE5F8ACE4F7ADE5F8ACE4      F7AAE2F5ABE3F60000009DE4F89DE4F89DE4F89DE4F89DE4F89DE4F89DE4F800      00009DE4F89DE4F89DE4F89DE4F89DE4F89DE4F89DE4F80000009BE4F89BE4F8      9BE4F89BE4F89BE4F89BE4F89BE4F80000009BE4F89BE4F89BE4F89BE4F89BE4      F89BE4F89BE4F80000009BE4F89BE4F89BE4F89BE4F89BE4F89BE4F89BE4F800      00009EE5F99EE5F99EE5F99EE5F99EE5F99EE5F99EE5F9000000A0E5F9A0E5F9      A0E5F9A0E5F9A0E5F9A0E5F9A0E5F9000000A1E4F9A1E4F9A1E4F9A1E4F9A1E4      F9A1E4F9A1E4F9000000A1E6F9A1E6F9A1E6F9A1E6F9A1E6F9A1E6F9A1E6F900      0000A2E6F9A2E6F9A2E6F9A2E6F9A2E6F9A2E6F9A2E6F9000000A2E6F9A2E6F9      A2E6F9A2E6F9A2E6F9A2E6F9A2E6F9000000A5E6FBA5E6FBA5E6FBA5E6FBA5E6      FBA5E6FBA5E6FB000000A8E7FBA8E7FBA8E7FBA8E7FBA8E7FBA8E7FBA8E7FB00      0000AAE7FBAAE7FBAAE7FBAAE7FBAAE7FBAAE7FBAAE7FB000000AAE8FAAAE8FA      AAE8FAAAE8FAAAE8FAAAE8FAAAE8FA000000ABE9F9ABE9F9ABE9F9ABE9F9ABE9      F9ABE9F9ABE9F9000000AEE9F9AEE9F9AEE9F9AEE9F9AEE9F9AEE9F9AEE9F900      0000B1E9FAB1E9FAB1E9FAB1E9FAB1E9FAB1E9FAB1E9FA000000B1E9FAB1E9FA      B1E9FAB1E9FAB1E9FAB1E9FAB1E9FA000000B4EAFBB4EAFBB4EAFBB4EAFBB4EA      FBB4EAFBB4EAFB000000B7EBFBB7EBFBB7EBFBB7EBFBB7EBFBB7EBFBB7EBFB00      0000B9ECFCB9ECFCB9ECFCB9ECFCB9ECFCB9ECFCB9ECFC000000B9ECFCB9ECFC      B9ECFCB9ECFCB9ECFCB9ECFCB9ECFC000000B9ECFCB9ECFCB9ECFCB9ECFCB9EC      FCB9ECFCB9ECFC000000C0EDFBC0EDFBC0EDFBC0EDFBC0EDFBC0EDFBC0EDFB00      0000C0EDFBC0EDFBC0EDFBC0EDFBC0EDFBC0EDFBC0EDFB000000C2EEFBC2EEFB      C2EEFBC2EEFBC2EEFBC2EEFBC2EEFB000000C3EFFCC3EFFCC3EFFCC3EFFCC3EF      FCC3EFFCC3EFFC000000C5EFFBC5EFFBC5EFFBC5EFFBC5EFFBC5EFFBC5EFFB00      0000C8F0FCC8F0FCC8F0FCC8F0FCC8F0FCC8F0FCC8F0FC000000CAF1FACAF1FA      CAF1FACAF1FACAF1FACAF1FACAF1FA000000CBF2FBCBF2FBCBF2FBCBF2FBCBF2      FBCBF2FBCBF2FB000000CEF1FBCEF1FBCEF1FBCEF1FBCEF1FBCEF1FBCEF1FB00      0000CEF1FBCEF1FBCEF1FBCEF1FBCEF1FBCEF1FBCEF1FB000000CFF2FCCFF2FC      CFF2FCCFF2FCCFF2FCCFF2FCCFF2FC000000D2F2FDD2F2FDD2F2FDD2F2FDD2F2      FDD2F2FDD2F2FD000000D2F3FCD2F3FCD2F3FCD2F3FCD2F3FCD2F3FCD2F3FC00      0000D4F3FCD4F3FCD4F3FCD4F3FCD4F3FCD4F3FCD4F3FC000000D4F3FCD4F3FC      D4F3FCD4F3FCD4F3FCD4F3FCD4F3FC000000D4F3FCD4F3FCD4F3FCD4F3FCD4F3      FCD4F3FCD4F3FC000000}  end

那么在实际的应用中如何实现把图片转16进制呢,可以参考以下转换PNG的例子,其他格式的图片转换都基本类似:

 

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;

请注意,这点特别重要:

在个别时候,可能需要解析From源代码中生成的16进制图片,可以参考以下代码:

粘贴的时候,要去掉开头的一小段类信息:具体要看自己保存出来的图片字符串

 

由于我采用的pngimage控件中未找到TPngImage类,我通过修改改成一下代码

在我的代码中,我加载到TImage控件中的代码为:

 

{从 png 图片到十六进制字符串}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; {从十六进制字符串恢复 png 图片}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;

 

 

 

 


procedure TForm1.Button4Click(Sender: TObject);
var png: TPngObject;begin //通过Image图片获取字符串 png := TPngObject.Create; try png.Assign(Image1.Picture.Graphic); Memo1.Text:=Png2Hex(png); finally png.Free; end;end;procedure TForm1.Button5Click(Sender: TObject);var png: TPngObject;begin //通过字符串显示到Image图片 png := TPngObject.Create; try Hex2Png(Memo1.Text,png); Image1.Picture.Graphic.Assign(png); finally png.Free; end;end;

 

太有用了,所以转:Delphi下16进制位图数据转位图