vb.net 教程 5-7 Bitmap类 3 获得图片信息Exif 2

上一节学习了如何获得Exif信息,并且获得Property的Type属性为2的ASCII 字符串信息。那么其他的一些信息怎么获得?
由于exif的大部分信息是为无符号的长整型对数组,每一对都表示一个分数;第一个整数是分子,第二个整数是分母。因此在此学习前先学习BitConverter 类的用法

BitConverter 类可以将基础数据类型与字节数组相互转换。
先看看以下两个代码段:
Private Sub Button2_Click(sender As Object,e As EventArgs) Handles Button2.Click
        Dim btetest() As Byte = {10,255,0}
        Dim intA As Integer = BitConverter.ToInt32(bteTest,0)
        Dim intB As Integer = BitConverter.ToInt32(bteTest,4)
        MessageBox.Show("intA=" & intA & " intB=" & intB)
    End Sub
运行结果:

使用BitConverter.ToInt32将4个字节转为Int32(Integer)的值。

 
 
Private Sub Button3_Click(sender As Object,e As EventArgs) Handles Button3.Click
        Dim btetest() As Byte = {&H10,&H2,2,4)
        MessageBox.Show("intA=" & intA & " intB=" & intB)
    End Sub
运行结果:
仍然是转为Int32的值,但是需要注意的是,通常情况字节顺序是高位在后:
&H10,&H2,0,0 =>210(十六进制) =>528(十进制)
255,2,0,0 =>2FF(十六进制) =>767(十进制)

回到 Bitmap. PropertyItems。
修改后的getPicInfo代码
Private Function getPicInfo(ByVal picFullPath As String) As String
        Dim bmp As New Bitmap(picFullPath)
        Dim strPro As String = ""
        For Each pro As Imaging.PropertyItem In bmp.PropertyItems
            Dim strTmp As String = ""
            Select Case pro.Type
                Case 1  
                    strTmp = "length:" & pro.Len
                Case 2
                    strTmp = System.Text.Encoding.ASCII.GetString(pro.Value).Replace(Chr(0),"")
                Case 3
                    strTmp = BitConverter.ToInt16(pro.Value,0)
                Case 4
                    strTmp = BitConverter.ToInt32(pro.Value,0)
                Case 5
                    strTmp = getvalue(pro.Value).ToString
                Case 6
                    strTmp = "type:" & pro.Type & " " & pro.Value.ToString
                Case 7
                    strTmp = "type:" & pro.Type & " " & pro.Value.ToString
                Case 8
                    strTmp = "type:" & pro.Type & " " & pro.Value.ToString
                Case 9
                    strTmp = "type:" & pro.Type & " " & pro.Value.ToString
                Case 10
                    strTmp = getvalue(pro.Value).ToString
                Case 11
                    strTmp = BitConverter.ToSingle(pro.Value,0)
                Case 12
                    strTmp = BitConverter.Todouble(pro.Value,0)
            End Select
            strPro &= pro.Id.ToString & "----" & pro.Len & "----" & pro.Type & "----" & strTmp & ControlChars.CrLf
        Next
        Return strPro
    End Function
其中调用的getValue代码
Private Function getvalue(ByVal bteValue() As Byte) As Single
        Dim intFirst As Integer
        Dim intSecond As Integer

        If bteValue.Length = 8 Then
            intFirst = BitConverter.ToInt32(bteValue,0)
            intSecond = BitConverter.ToInt32(bteValue,4)
            Return intFirst / intSecond
        End If
        Return 0
    End Function
运行结果:

msdn上也对PropertyItem的Id的含义做了说明,更详细的说明可以参看:https://msdn.microsoft.com/zh-cn/library/ms534416(v=vs.85).aspx#_gdiplus_constant_propertytagexiffnumber
例如:33437十六进制值为:829D,对应的是相机光圈

需要注意的是,不同的id,即使propertyitem.type相同,所包含的数据也可能不同。

最后再次吐槽csdn的编辑器。居然编死了几次,害得我不得不再次打字录入,提心吊胆的。

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看vb.net 教程 目录

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...