从图像文件中获取MetaData

时间:2017-08-22 21:32:35

标签: wpf image-processing

我们有存储在数据库中的图像,它们用于替换Word文档中的图像 - 该位工作正常,除非替换图像是纵向并且它正在取代横向图像,所以我试图获得用于确定如何使用此函数定向图像的元数据

Public Function GetImageTags(ImageFile() As Byte) As String()
    Try
        Dim vReturnArray() As String = Nothing
        Using MS As New System.IO.MemoryStream(ImageFile)
            Dim vDecoder As BitmapDecoder = BitmapDecoder.Create(MS, BitmapCreateOptions.None, BitmapCacheOption.Default)
            Dim vFrame As BitmapFrame = vDecoder.Frames(0)
            Dim vMetadata As BitmapMetadata = TryCast(vFrame.Metadata, BitmapMetadata)
            If vMetadata IsNot Nothing And vMetadata.Keywords IsNot Nothing Then
                vReturnArray = vMetadata.Keywords.ToArray()
            End If
        End Using
        Return vReturnArray
    Catch ex As Exception
        EmailError(ex)
        Return Nothing
    End Try
End Function

......但它会把玩具扔出去......

This codec does not support the specified property.
at System.Windows.Media.Imaging.BitmapMetadata.GetQuery(String query)
at System.Windows.Media.Imaging.BitmapMetadata.get_Keywords()

...在BitMapMetadata.Keywords。知道如何克服这个问题并获得关键字吗?

谢谢

================更新================

看来错误,我也试过......

vReturnArray = TryCast(vMetadata.GetQuery("System.Keywords"), String())

...仅针对某些图片返回,但我尝试的所有内容都为String()返回了Nothing

2 个答案:

答案 0 :(得分:0)

Code Project上有一个非常好的EXIF类很容易实现,或者使用指向文件的字符串链接

 Dim vEXIF As New ImageEXIF(ImagePath)
            Dim vOrientation As Integer = vEXIF.Orientation

或作为BitMap

 Dim vOrientation As Integer = 0
                    Using vBitmap As System.Drawing.Image = System.Drawing.Image.FromStream(New IO.MemoryStream(ImageFile))
                        Dim vEXIF As New ImageEXIF(vBitmap)
                        vOrientation = vEXIF.Orientation
                    End Using

将另一个Sub添加到Byte()的类中并不困难,但上面的转换非常简单,并且该类应该适用于所有图像类型。

答案 1 :(得分:0)

您可以使用MetadataExtractor访问图片元数据。

检查任何包含的Exif目录中是否存在ExifDirectoryBase.TagOrientation

这样的事情(对不起它的C#因为我不懂VB.NET):

var orientation = ImageMetadataReader.ReadMetadata(imagePath)
    .OfType<ExifSubIfdDirectory>()
    .Select(d => d.GetObject(ExifDirectoryBase.TagOrientation))
    .First(o => o != null);