2015-03-13 138 views
6

我需要爲上傳的圖像添加元數據標籤(說明)。將元數據寫入JPG和PNE

我已經發現這個答案:https://stackoverflow.com/a/1764913/6776它適用於JPG文件,但不適用於PNG。

private string Tag = "test meta data"; 

private static Stream TagImage(Stream input, string type) 
{ 
    bool isJpg = type.EndsWith("jpg", StringComparison.InvariantCultureIgnoreCase) || type.EndsWith("jpeg", StringComparison.InvariantCultureIgnoreCase); 
    bool isPng = type.EndsWith("png", StringComparison.InvariantCultureIgnoreCase); 

    BitmapDecoder decoder = null; 

    if (isJpg) 
    { 
     decoder = new JpegBitmapDecoder(input, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); 
    } 
    else if (isPng) 
    { 
     decoder = new PngBitmapDecoder(input, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); 
    } 
    else 
    { 
     return input; 
    } 

    // modify the metadata 
    BitmapFrame bitmapFrame = decoder.Frames[0]; 
    BitmapMetadata metaData = (BitmapMetadata)bitmapFrame.Metadata.Clone(); 
    metaData.Subject = Tag; 
    metaData.Comment = Tag; 
    metaData.Title = Tag; 

    // get an encoder to create a new jpg file with the new metadata.  
    BitmapEncoder encoder = null; 
    if (isJpg) 
    { 
     encoder = new JpegBitmapEncoder(); 
    } 
    else if (isPng) 
    { 
     encoder = new PngBitmapEncoder(); 
    } 

    encoder.Frames.Add(BitmapFrame.Create(bitmapFrame, bitmapFrame.Thumbnail, metaData, bitmapFrame.ColorContexts)); 

    // Save the new image 
    Stream output = new MemoryStream(); 
    encoder.Save(output); 

    output.Seek(0, SeekOrigin.Begin); 

    return output; 
} 

當我上傳JPG它的偉大工程,但有一個PNG,在metaData.Subject = Tag線,它拋出一個System.NotSupportedException(此編解碼器不支持指定的屬性)。

更新

看來我必須使用基於圖像格式的不同方法:

if (isJpg) 
{ 
    metaData.SetQuery("/app1/ifd/exif:{uint=270}", Tag); 
} 
else 
{ 
    metaData.SetQuery("/tEXt/{str=Description}", Tag); 
} 

基於the available formats' queries首先應該對兩種格式的工作。第二個也沒有效果(它在圖像中創建元數據但不保存其值)。

如果我嘗試使用PNG的第一種方法(/app1/ifd/exif),在encoder.Save這一行,我得到一個不支持的異常「沒有適合的成像組件」。

+0

不是你的問題相關,但我認爲你的isJpg =語句有錯誤。我假設你想測試「.jpg」或「.jpeg」,但是你測試兩次「.jpg」。 – RenniePet 2015-03-16 11:28:02

+0

是的,從那時起它已經在代碼中修復了,但不是在問題中。謝謝! – thomasb 2015-03-16 11:43:46

回答

-1

的PNG格式不支持元數據:(

XMP確實,PNG JPEG格式之間進行轉換時,有EXIF的元數據,以及可能會有幫助。

+0

根據Wikipedia上的[Portable Network Graphics](https://en.wikipedia.org/wiki/Portable_Network_Graphics#Ancillary_chunks)主題,PNG可以存儲元數據。所以我不確定你爲什麼認爲它不能。 – 2015-07-06 15:38:25

+1

我懷疑是因爲'PngBitmapEncoder'的元數據成員是不可設置的。特別是'encoder.Metadata = new BitmapMetadata(「png」);'產生一個異常「指定的BitmapEncoder不支持全局元數據。」 – Eponymous 2016-08-20 22:19:20

2

我解決了使用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); 
     } 


    } 
}