You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi 11 Alexandria如何读取PNG中添加的tEXt元数据?

在Delphi 11 Alexandria中读取PNG的tEXt块数据

TPngImage并没有单独的"读取tEXt"方法,而是通过**Text属性**来访问所有已添加的键值对数据——这个属性是一个TStringList,其中每个条目都以Key:Value的格式存储你通过AddtEXt添加的内容。

读取指定键的值

你可以利用TStringList的IndexOfName和ValueFromName方法直接获取目标键对应的值:

procedure TFrmMain.LoadPNGtEXtClick(Sender: TObject);
var
  Png: TPngImage;
  TargetValue: string;
  KeyIndex: Integer;
begin
  if OpenDialog1.Execute then
  begin
    Png := TPngImage.Create;
    try
      Png.LoadFromFile(OpenDialog1.FileName);
      // 查找指定键的索引
      KeyIndex := Png.Text.IndexOfName('SomeKey');
      if KeyIndex <> -1 then
      begin
        TargetValue := Png.Text.ValueFromName('SomeKey');
        ShowMessage('读取到的值:' + TargetValue);
      end
      else
      begin
        ShowMessage('未找到指定的键:SomeKey');
      end;
    finally
      Png.Free;
    end;
  end;
end;

遍历所有tEXt键值对

如果需要导出所有已存储的tEXt数据,可以直接遍历Text列表:

procedure TFrmMain.ListAllPNGtEXtClick(Sender: TObject);
var
  Png: TPngImage;
  I: Integer;
  Key, Value: string;
begin
  if OpenDialog1.Execute then
  begin
    Png := TPngImage.Create;
    try
      Png.LoadFromFile(OpenDialog1.FileName);
      Memo1.Clear;
      for I := 0 to Png.Text.Count - 1 do
      begin
        Key := Png.Text.Names[I];
        Value := Png.Text.ValueFromIndex[I];
        Memo1.Lines.Add(Format('键:%s,值:%s', [Key, Value]));
      end;
    finally
      Png.Free;
    end;
  end;
end;

注意事项

  • Text属性中的每个条目严格遵循Key:Value格式,冒号是默认的分隔符,和你调用AddtEXt(Key, Value)时的参数对应。
  • 如果你的键名称中包含冒号,IndexOfName和ValueFromName可能无法正确解析,这种情况需要手动处理字符串分割。

内容的提问来源于stack exchange,提问作者stevenvh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 04:10:14