上一节学习了如何获得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)的值。
使用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(十进制)
255,2,0,0 =>2FF(十六进制) =>767(十进制)
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
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 教程 目录