2010-08-27 27 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); 
     } 


    } 
}