• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

如果我们在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;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
从Delphi7升级到DelphiXE发布时间:2022-07-18
下一篇:
delphi创建一个纯文本文件发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap