TPNGImage help

PNG Delphi

Example 1: Reading textual chunks


Create a new form and insert an edit box, a listbox, a memo and a button. The edit box is supposed to receive the file name, the listbox will contain all the keywords for the textual chunks. The memo will contain the text for the selected keyword in the listbox. And finally the button will load the file and fill the listbox. Use the code bellow:


uses
  Forms, pngimage, StdCtrls, Classes, Controls,
    Dialogs;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    Memo1: TMemo;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  public
   
png: TPngObject;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

{$R *.DFM}

var

  Form1: TForm1;

implementation


{Form being created, create the png object}
constructor TForm1.Create(AOwner: TComponent);
begin
  inherited
Create(AOwner); 
 
png := tpngobject.create;
end;


{Form being destroyed, destroy the png object}
destructor TForm1.Destroy;
begin
  inherited
Destroy; 
 
png.free;
end;


{User clicked on the button, load the file and fill list}
procedure TForm1.Button1Click(Sender: TObject);
var
 
i: Integer;
begin

  try
   
{Load the png file into the object}
   
png.LoadFromFile(Edit1.Text);
    {Clear the listbox}
    listbox1.items.clear;
    {Searches for all the chunks using the type TChunktEXt}
    {add these to the listbox and a pointer to the chunk}
    {Note that all textual chunks are descendent from
    {TChunktEXt}

    for i := 0 to png.chunks.count - 1 do
      if
png.chunks.item[i] is TChunktEXt then
        listbox1.Items.AddObject(TChunktEXt(
          png.chunks.item[i]).keyword, png.chunks.item[i]);
  except
    {In case the image could not be loaded, show error}
    showmessage('The file could not be loaded.');
  end;
end;

{User selected an item, show text on the memo}
procedure TForm1.Listbox1Click(Sender: TObject);
begin
  if listbox1.itemindex <> -1 then
    memo1.text := TChunktEXt(
      Listbox1.Items.Objects[Listbox1.itemindex]).Text;
end;