1、引入.net framework3.0中的PresentationCore.dll以及WindowsBase.dll
2、创建IPTC抽取器类IPTCMetadataExtractor(代码如下)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public class IPTCMetadataExtractor
{
public static BitmapMetadata GetBitmapMetadata(string imageFilePath)
{
if (String.IsNullOrEmpty(imageFilePath))
return null;
BitmapDecoder fileBitmapDecoder;
using (Stream stream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
try
{
fileBitmapDecoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
// DO NOT USE: fileBitmapDecoder = BitmapDecoder.Create(new Uri(imageFilePath, UriKind.Absolute), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
catch (NotSupportedException)
{
// Some image types, such as wmf, throw an exception. Let's skip trying to get the metadata.
return null;
}
}
if ((fileBitmapDecoder == null) || (fileBitmapDecoder.Frames.Count == 0))
return null;
BitmapFrame fileFirstFrame = fileBitmapDecoder.Frames[0];
if (fileFirstFrame == null)
return null;
BitmapDecoder firstFrameBitmapDecoder = fileFirstFrame.Decoder;
if ((firstFrameBitmapDecoder == null) || (firstFrameBitmapDecoder.Frames.Count == 0))
return null;
BitmapFrame firstFrameInDecoderInFirstFrameOfFile = firstFrameBitmapDecoder.Frames[0];
// The Metadata property is of type ImageMetadata, so we must cast it to BitmapMetadata.
return firstFrameInDecoderInFirstFrameOfFile.Metadata as BitmapMetadata;
}
public static string ConvertStringCollectionToDelimitedString(System.Collections.ObjectModel.ReadOnlyCollection<string> stringCollection)
{
string delimiter = "; ";
string[] strings = new string[stringCollection.Count];
stringCollection.CopyTo(strings, 0);
return String.Join(delimiter, strings);
}
}3、传入图片路径参数,获取元数据类
BitmapMetadata metadata = IPTCMetadataExtractor.GetBitmapMetadata(picPath);
4、可以通过
BitmapMetadata类自带的属性直接获取IPTC信息,如
metadata.Author获取作者信息,metadata.Comment获取摘要信息等等。这种方式获取的IPTC信息不用显示的区分图片的类型,
BitmapMetadata已经自动为我们做了处理。
5、有时候,
BitmapMetadata的属性没有包含我们需要的信息,这就需要我们使用
BitmapMetadata提供的GetQuery方法获取指定的IPTC信息。这种情况下,我们要显示的区分图片类型。
获取JPG的IPTC代码,如下代码获取JPG图片的Headline:
metadata.GetQuery("/app13/irb/8bimiptc/iptc/Headline");
获取TIF的IPTC代码,如下代码获取TIF图片的Headline:
metadata.GetQuery("/ifd/iptc/headline");
6、可以通过
BitmapMetadata的Format属性判断图片的类型。
7、以下是一些JPG的QueryPath:
/app13/irb/8bimiptc/iptc/Headline
/app13/irb/8bimiptc/iptc/Title
/app13/irb/8bimiptc/iptc/Caption
/app13/irb/8bimiptc/iptc/City
8、以下是一些TIF的QueryPath:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
/ifd/iptc/city
/ifd/iptc/by-line
/ifd/iptc/by-line title
/ifd/iptc/caption
/ifd/iptc/copyright notice
/ifd/iptc/object name
/ifd/iptc/headline
/ifd/iptc/keywords
/ifd/iptc/date created
/ifd/iptc/special instructions
/ifd/iptc/credit
/ifd/iptc/source
/ifd/iptc/original transmission reference
/ifd/iptc/country\\/primary location name
/ifd/iptc/province\\/state
/ifd/iptc/writer\\/editor
/ifd/iptc/location依此类推……