代码之家  ›  专栏  ›  技术社区  ›  Rowland Shaw

从WPF中的图像读取元数据

  •  4
  • Rowland Shaw  · 技术社区  · 17 年前

    我知道WPF允许您使用需要WIC编解码器才能查看的图像(为了便于讨论,比如数码相机原始文件);然而,我只能看到它让您以本机方式显示图像,但我看不到获取元数据的任何方法(例如,曝光时间)。

    正如Windows资源管理器所示,这显然是可以做到的,但这是通过.net API公开的,还是您认为这仅仅是调用本机COM接口

    2 回复  |  直到 17 年前
        1
  •  12
  •   Clemens    6 年前

    查看我的 Intuipic BitmapOrientationConverter 类,该类读取元数据以确定图像的方向:

    private const string _orientationQuery = "System.Photo.Orientation";
    ...
    
    using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
    {
        BitmapFrame bitmapFrame = BitmapFrame.Create(fileStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
        BitmapMetadata bitmapMetadata = bitmapFrame.Metadata as BitmapMetadata;
    
        if ((bitmapMetadata != null) && (bitmapMetadata.ContainsQuery(_orientationQuery)))
        {
            object o = bitmapMetadata.GetQuery(_orientationQuery);
    
            if (o != null)
            {
                //refer to http://www.impulseadventure.com/photo/exif-orientation.html for details on orientation values
                switch ((ushort) o)
                {
                    case 6:
                        return 90D;
                    case 3:
                        return 180D;
                    case 8:
                        return 270D;
                }
            }
        }
    }
    
        2
  •  4
  •   Drew Noakes    8 年前

    虽然WPF确实提供了这些API,但它们不是非常友好,速度也不是特别快。我怀疑他们做了很多互操作。

    我有一个 simple open-source library

    // Read all metadata from the image
    var directories = ImageMetadataReader.ReadMetadata(stream);
    
    // Find the so-called Exif "SubIFD" (which may be null)
    var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
    
    // Read the orientation
    var orientation = subIfdDirectory?.GetInt(ExifDirectoryBase.TagOrientation);
    
    switch (orientation)
    {
        case 6:
            return 90D;
        case 3:
            return 180D;
        case 8:
            return 270D;
    }
    

    在我的基准测试中,这比WPF API快17倍。如果您只希望使用JPEG格式的Exif,请使用以下格式,速度快30倍以上:

    var directories = JpegMetadataReader.ReadMetadata(stream, new[] { new ExifReader() });
    

    元数据提取器 图书馆可通过 NuGet code's on GitHub .

    推荐文章