2010-08-27 79 views
1

我有一个PNG文件,我想每单位添加的属性如何将属性添加到PNG文件

  1. 像素,X轴单位
  2. 像素,Y轴
  3. 单位符:米

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

我有PROGR通过读取png的属性来检查属性是否存在,这样我就可以设置这个属性的值,但是我在png文件中看不到这个属性。 (参考pixel-per-unit.JPG)

我们如何添加属性到png文件中?

问候 alt text

回答

0

我认为你正在寻找SetPropertyItem。你可以找到财产ID here

你会使用属性ID来获取,然后设置属性项为您的元数据。

编辑

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

  1. 0x5111 - 像素每单位X
  2. 0x5112 - 像素每部件Y5
  3. 0x5110 - 像素单位
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); 
     } 


    } 
}