【问题标题】:How to add a property to a PNG file如何将属性添加到 PNG 文件
【发布时间】:2011-04-04 22:01:12
【问题描述】:

我有一个要添加属性的 PNG 文件

  1. X 轴单位像素数
  2. 每单位像素数,Y 轴
  3. 单位说明符:米

PNG 规范中解释了这些属性:http://www.w3.org/TR/PNG-Chunks.html

我以编程方式读取了.png 的属性以检查属性是否存在,以便我可以设置此属性的值,但我在.png 文件中看不到此属性。 (参考每单位像素.JPG)

我们如何向.png 文件添加属性?

问候

【问题讨论】:

    标签: c# .net png file-properties


    【解决方案1】:

    尝试使用pngcs库(需要将下载的dll重命名为“pngcs.dll”)

    我需要添加一些自定义文本属性,但您可以轻松完成更多工作。

    这是我添加自定义文本属性的实现:

    using Hjg.Pngcs;  // https://code.google.com/p/pngcs/
    using Hjg.Pngcs.Chunks;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MarkerGenerator.Utils
    {
        class PngUtils
        {
    
            public string getMetadata(string file, string key)
            {
    
                PngReader pngr = FileHelper.CreatePngReader(file);
                //pngr.MaxTotalBytesRead = 1024 * 1024 * 1024L * 3; // 3Gb!
                //pngr.ReadSkippingAllRows();
                string data = pngr.GetMetadata().GetTxtForKey(key);
                pngr.End();
                return data; ;
            }
    
    
            public static void addMetadata(String origFilename, Dictionary<string, string> data)
            {
                String destFilename = "tmp.png";
                PngReader pngr = FileHelper.CreatePngReader(origFilename); // or you can use the constructor
                PngWriter pngw = FileHelper.CreatePngWriter(destFilename, pngr.ImgInfo, true); // idem
                //Console.WriteLine(pngr.ToString()); // just information
                int chunkBehav = ChunkCopyBehaviour.COPY_ALL_SAFE; // tell to copy all 'safe' chunks
                pngw.CopyChunksFirst(pngr, chunkBehav);          // copy some metadata from reader 
                foreach (string key in data.Keys)
                {
                    PngChunk chunk = pngw.GetMetadata().SetText(key, data[key]);
                    chunk.Priority = true;
                }
    
                int channels = pngr.ImgInfo.Channels;
                if (channels < 3)
                    throw new Exception("This example works only with RGB/RGBA images");
                for (int row = 0; row < pngr.ImgInfo.Rows; row++)
                {
                    ImageLine l1 = pngr.ReadRowInt(row); // format: RGBRGB... or RGBARGBA...
                    pngw.WriteRow(l1, row);
                }
                pngw.CopyChunksLast(pngr, chunkBehav); // metadata after the image pixels? can happen
                pngw.End(); // dont forget this
                pngr.End();
                File.Delete(origFilename);
                File.Move(destFilename, origFilename);
    
            }
    
            public static void addMetadata(String origFilename,string key,string value)
            {
                Dictionary<string, string> data = new Dictionary<string, string>();
                data.Add(key, value);
                addMetadata(origFilename, data);
            }
    
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      我想你正在寻找SetPropertyItem。您可以找到属性 ID here

      您将使用属性 id 来获取然后为您的元数据设置属性项。

      编辑

      你需要的三个id(我认为)是:

      1. 0x5111 - 每单位像素 X
      2. 0x5112 - 每单位像素 Y
      3. 0x5110 - 像素单位

      【讨论】:

        猜你喜欢
        • 2012-11-20
        • 2018-08-25
        • 2020-03-13
        • 2013-11-08
        • 1970-01-01
        • 2014-07-22
        • 2018-02-10
        • 2017-04-22
        • 2021-04-06
        相关资源
        最近更新 更多