如果我们在Form中拖入一个Image控件,并设置好picture后,Alt+F12就可以看到Form的源代码中已经将图片转成了16进制字符串,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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
AAE8FAAAE8FAAAE8FAAAE8FAAAE8FA000000ABE9F9ABE9F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D4F3FC
D4F3FCD4F3FCD4F3FCD4F3FCD4F3FC000000D4F3FCD4F3FCD4F3FCD4F3FCD4F3
FCD4F3FCD4F3FC000000}
end
|
那么在实际的应用中如何实现把图片转16进制呢,可以参考以下转换PNG的例子,其他格式的图片转换都基本类似:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
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进制图片,可以参考以下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
BtnImg1 := TBitmap . Create;
MS1 := TMemoryStream . Create;
MSHexdata1 := '424D660200000000000036000000280000000500' +
'000023000000010018000000000030020000C40E0000C40E0000000000000000' +
'0001CE8505CE8505CE8505CE8505CE850500CE8505CE8505CE8505CE8505CE85' +
'0500EDAA34EDAA34EDAA34EDAA34EDAA3400EDAA35EDAA35EDAA35EDAA35EDAA' +
'3500EDAA35EDAA35EDAA35EDAA35EDAA3500EEAB36EEAB36EEAB36EEAB36EEAB' +
'3600EEAB36EEAB36EEAB36EEAB36EEAB3600EEAC37EEAC37EEAC37EEAC37EEAC' +
'3700EFAC37EFAC37EFAC37EFAC37EFAC3700EFAD38EFAD38EFAD38EFAD38EFAD' +
'3800F0AD38F0AD38F0AD38F0AD38F0AD3800F0AE39F0AE39F0AE39F0AE39F0AE' +
'3900F0AE39F0AE39F0AE39F0AE39F0AE3900F1AF3AF1AF3AF1AF3AF1AF3AF1AF' +
'3A00F2B03AF2B03AF2B03AF2B03AF2B03A00F2B03AF2B03AF2B03AF2B03AF2B0' +
'3A00F3B13CF3B13CF3B13CF3B13CF3B13C00F3B13CF3B13CF3B13CF3B13CF3B1' +
'3C00F4B23DF4B23DF4B23DF4B23DF4B23D00F5B33EF5B33EF5B33EF5B33EF5B3' +
'3E00F5B33EF5B33EF5B33EF5B33EF5B33E00F6B43FF6B43FF6B43FF6B43FF6B4' +
'3F00F6B43FF6B43FF6B43FF6B43FF6B43F00F7B540F7B540F7B540F7B540F7B5' +
'4000F7B641F7B641F7B641F7B641F7B64100F8B641F8B641F8B641F8B641F8B6' +
'4100F8B742F8B742F8B742F8B742F8B74200F9B742F9B742F9B742F9B742F9B7' +
'4200F9B843F9B843F9B843F9B843F9B84300F9B843F9B843F9B843F9B843F9B8' +
'4300FAB844FAB844FAB844FAB844FAB84400FAB944FAB944FAB944FAB944FAB9' +
'4400FAB944FAB944FAB944FAB944FAB94400FBBF53FBBF53FBBF53FBBF53FBBF' +
'5300FCCA6FFCCA6FFCCA6FFCCA6FFCCA6F00' ;
MS1 . SetSize(Length(MSHexdata1) div 2 );
HexToBin( PChar (MSHexdata1),MS1 . Memory,MS1 . Size);
MS1 . SaveToFile( 'c:\1.bmp' );
BtnImg1 . LoadFromStream(MS1);
|
MSHexdata1 的值就是直接从Form源代码中粘贴出来的,需要注意的是:
粘贴的时候,要去掉开头的一小段类信息:
如本例中就已经去掉了开头的“07544269746D61703E040000”,这段信息转换成ASCII如下图:
并不是图像信息的一部分,而是delphi的标记,因此需要去掉,否则生成的图像将无法显示。