利用BitmapMetadata读取JPG和TIF的IPTC信息

1、引入.net framework3.0中的PresentationCore.dll以及WindowsBase.dll
2、创建IPTC抽取器类IPTCMetadataExtractor(代码如下)
ContractedBlock.gif ExpandedBlockStart.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:
ContractedBlock.gif ExpandedBlockStart.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
依此类推……

转载于:https://www.cnblogs.com/lethe2046/archive/2009/09/14/1566389.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值