In my Delphi app I create a PNG file which also includes some text as key-value pairs:
procedure TFrmMain.SaveasPNG1Click(Sender: TObject);
var
Png: TPngImage;
begin
if SaveDialog1.Execute then
begin
Png := TPngImage.Create;
try
Png.Assign(Image1.Picture.Graphic);
Png.AddtEXt('SomeKey', 'SomeValue'); // <-- adding text
Png.SaveToFile(SaveDialog1.FileName);
finally
Png.Free;
end;
end;
end;
I can't seem to find any TPNGimage method to read the data back, however. Is there a way to read the data back in my app?
(I'm using Delphi 11 Alexandria)
When you call
Png.AddtEXt()a new data chunk of typeTChunkTEXTis created and your data assigned to it.You can retrieve such data back from the PNG by finding this data chunk using the
Png.Chunks.FindChunk()method where you pass theTChunkTEXTclass as parameter to the mentioned method. The code for retrieving such data would look like this:The only problem of the
.FindChunk()method is that it only finds the first chunk matching the specified class - therefore you can retrieve only the first key-value pair even although it is possible to save multiple key-value pairs by callingPng.AddtEXt()multiple times.In order to retrieve multiple key-value pairs from a PNG you will have to iterate through all chunks manually and retrieve data from text chunks. The code should look like this:
PS: Yes the documentation on
TPNGimageis a bit lacking but you can find how it works by peaking into theVcl.Imaging.pngimageunit that ships with Delphi.