1、前言
需要把PNG图片转化成数组的形式,供系统显示。但是delphi不支持直接处理PNG图片,所以需要第三方插件来实现。我使用的TPNGImage,接下针对TPNGImage组件及使用进行说明。
组件下载地址
2、TPNGImage组件常用属性说明
Height: 图片的高度。
Width:图片的宽度。
AlphaScanline:提供透明度信息;原型:AlphaScanline[const Index: Integer]: pByteArray; 参数说明:Index是指行数,最大值为图片的高度;类型值是指向Index行的Alpha值的指针,指向长度等于PNG图片的宽度,当PNG图片没有透明度时返回null.
Scanline:根据图片位深和颜色类型提供RGB或ARGB信息;原型:Scanline[const Index: Integer]: Pointer;参数说明:Index是指行数,最大值为图片的高度;类型值是指向Index行的RGB值的指针。验证没有得到Alpha值,不知道为什么,直接用了Pixels.
Pixels:提供RGB信息;原型:Pixels[const X, Y: Integer]: TColor; 参数说明:X横坐标值;Y纵坐标值;类型值是指定坐标的RGB值,值得注意,字节对应关系是从低字节到高字节是RGB。
Header:主要用到BitDepth和ColorType;
其他属性暂时没用到,感兴趣的小伙伴自行挖掘。
3.TPNGImage组件常用属性的使用
function Get_Png(Png_File_Name:string):string;
var
pPng_Buf:PByteArray;
Png_FileName, Png_Dis_Str:string;
Png_x, Png_y:word;
Png_R, Png_G, Png_B:Byte;
begin
MyPng := TPNGObject.Create;
MyPng.LoadFromFile(Png_File_Name);
Png_Dis_Str := 'Height: ' + IntToStr(MyPng.Height) + #13#10;
Png_Dis_Str := Png_Dis_Str + 'Width: ' + IntToStr(MyPng.Width) + #13#10;
Png_Dis_Str := Png_Dis_Str + 'ColorType: ' + IntToStr(MyPng.Header.ColorType) + #13#10;
Png_Dis_Str := Png_Dis_Str + 'BitDepth: ' + IntToStr(MyPng.Header.BitDepth) + #13#10;
for Png_y := 0 to MyPng.Height - 1 do
begin
pPng_Buf := MyPng.AlphaScanline[Png_y];
for Png_x := 0 to MyPng.width - 1 do
begin
//Png_R := Byte(MyPng.Pixels[Png_x, Png_y]);
//Png_G := Byte(MyPng.Pixels[Png_x, Png_y] shr 8);
//Png_B := Byte(MyPng.Pixels[Png_x, Png_y] shr 16);
Png_Dis_Str := Png_Dis_Str + '0x' + IntToHex(MyPng.Pixels[Png_x, Png_y], 4) + ', ';
end;
Png_Dis_Str := Png_Dis_Str + #13#10;
end;
MyPng.Free;
result := Png_Dis_Str;
end
本文介绍了如何在Delphi7中处理PNG图片,由于Delphi7本身不支持PNG格式,因此需要借助TPNGImage组件。文中详细讲解了TPNGImage的Height、Width、AlphaScanline、Scanline和Pixels等属性的用法,帮助开发者将PNG图片转化为数组形式供系统显示。
1569

被折叠的 条评论
为什么被折叠?



