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

本文介绍如何使用VB.NET解析图片中的EXIF信息,包括利用BitConverter类转换不同类型的数据,并针对不同的PropertyItem.Type属性展示其对应的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

上一节学习了如何获得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, 0, 0, 0, 255, 0, 0, 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, 0, 0, 255, 2, 0, 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

 

运行结果:

仍然是转为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 教程 目录

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.Net学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值