ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 3635|回复: 4

[求助] EXCEL VBA如何获取TIF影像的尺寸

[复制链接]

TA的精华主题

TA的得分主题

发表于 2016-1-17 13:03 | 显示全部楼层 |阅读模式
本帖已被收录到知识树中,索引项:图像处理和GDI
如图 我需要箭头所指的数据
QQ截图20160117130312.png

TA的精华主题

TA的得分主题

发表于 2016-1-17 21:32 | 显示全部楼层
blog.sina.cn/dpool/blog/s/blog_777d52410101p3z4.html?

TA的精华主题

TA的得分主题

发表于 2016-1-18 23:07 | 显示全部楼层
  1. Option Explicit

  2. Private Type ImageFileHeader
  3.     ByteOrder As Integer
  4.     Version As Integer
  5.     Offset(3) As Byte
  6. End Type

  7. Private Type ImageFileDictionary
  8.     Identifier(1) As Byte
  9.     FieldType(1) As Byte
  10.     Count(3) As Byte
  11.     ValueOffset(3) As Byte
  12. End Type

  13. Private Type TiffDimension
  14.     Width As Long
  15.     Height As Long
  16. End Type

  17. Function GetTiffDimension(tiffFile As String) As TiffDimension '主函数,根据tif文件名返回图像尺寸
  18.     Dim iFreeFile As Integer
  19.     Dim ifh As ImageFileHeader
  20.     Dim ifd() As ImageFileDictionary
  21.     Dim bLittleEndian As Boolean
  22.     Dim arrBuffer(1) As Byte
  23.     Dim iIFHcount As Integer
  24.     Dim i As Integer
  25.    
  26.     Dim lOffset As Long
  27.         
  28.     iFreeFile = FreeFile
  29.     Open tiffFile For Binary As iFreeFile
  30.         Get #iFreeFile, 1, ifh
  31.         If ifh.ByteOrder = &H4949 Then  '"II" = LittleEndian
  32.             bLittleEndian = True
  33.         ElseIf ifh.ByteOrder = &H4D4D Then  '"MM" = BigEndian
  34.             bLittleEndian = False
  35.         Else
  36.             MsgBox "invalid TIFF format"
  37.             Exit Function
  38.         End If
  39.         
  40.         lOffset = BytesToLong(ifh.Offset, bLittleEndian)
  41.         Get iFreeFile, lOffset + 1, arrBuffer
  42.         iIFHcount = BytesToUInt(arrBuffer, bLittleEndian)
  43.         ReDim ifd(iIFHcount - 1)
  44.         Get iFreeFile, lOffset + 3, ifd
  45.         For i = 0 To iIFHcount - 1
  46.             If BytesToUInt(ifd(i).Identifier, bLittleEndian) = &H100 Then 'image Width
  47.                 If BytesToUInt(ifd(i).FieldType, bLittleEndian) = 3 Then   '3=Int类型
  48.                     GetTiffDimension.Width = BytesToUInt(ifd(i).ValueOffset, bLittleEndian)
  49.                 ElseIf BytesToUInt(ifd(i).FieldType, bLittleEndian) = 4 Then '4=Long类型
  50.                     GetTiffDimension.Width = BytesToLong(ifd(i).ValueOffset, bLittleEndian)
  51.                 End If
  52.             ElseIf BytesToUInt(ifd(i).Identifier, bLittleEndian) = &H101 Then  'image Height
  53.                 If BytesToUInt(ifd(i).FieldType, bLittleEndian) = 3 Then
  54.                    GetTiffDimension.Height = BytesToUInt(ifd(i).ValueOffset, bLittleEndian)
  55.                 ElseIf BytesToUInt(ifd(i).FieldType, bLittleEndian) = 4 Then
  56.                    GetTiffDimension.Height = BytesToLong(ifd(i).ValueOffset, bLittleEndian)
  57.                 End If
  58.             End If
  59.         Next
  60.         
  61.     Close iFreeFile
  62.    
  63. End Function

  64. Function BytesToUInt(b() As Byte, Optional LittleEndian As Boolean = True) As Long  'VBA中没有Unsigned Int数据类型,所以用Long作返回值,避免出生溢出错误
  65.     If LittleEndian Then
  66.         BytesToUInt = CLng(b(1)) * &H100 + b(0)
  67.     Else
  68.         BytesToUInt = CLng(b(0)) * &H100 + b(1)
  69.     End If
  70. End Function

  71. Function BytesToLong(b() As Byte, Optional LittleEndian As Boolean = True) As Long
  72.     If LittleEndian Then
  73.         BytesToLong = CLng(b(3)) * &H1000000 + b(2) * &H10000 + b(1) * &H100 + b(0)
  74.     Else
  75.         BytesToLong = CLng(b(0)) * &H1000000 + b(1) * &H10000 + b(2) * &H100 + b(3)
  76.     End If
  77. End Function

  78. Sub GetDimension()  '测试代码
  79.     Dim gfd As TiffDimension
  80.     Dim sFileName As String
  81.     Dim iRow As Integer
  82.     sFileName = Application.GetOpenFilename("tif格式图片(*.tif),*.tif", , "请选择要查看的tif文件")
  83.     If sFileName = "False" Then Exit Sub
  84.     gfd = GetTiffDimension(sFileName)
  85.     iRow = Sheet1.Range("a65536").End(xlUp).Row + 1
  86.     Sheet1.Cells(iRow, 1) = sFileName
  87.     Sheet1.Cells(iRow, 2) = gfd.Width
  88.     Sheet1.Cells(iRow, 3) = gfd.Height
  89. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2018-11-27 09:49 | 显示全部楼层
学无止境,山外有山;高山仰止,信步登攀。

TA的精华主题

TA的得分主题

发表于 2018-11-27 10:05 | 显示全部楼层
下面就可以实现了。
Sub ts()
Dim w As Object
Set w = VBA.CreateObject("wia.imagefile")
w.loadfile "C:\Users\Fanxiaolei\Desktop\timg.tif"
MsgBox "图分辨率为:" & w.Width & "X" & w.Height
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-27 05:24 , Processed in 0.038353 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表