分享
 
 
 

Calculate height and width of GIF/JPG files

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

这个是转载

Function ImageSize(fileName As String) As Variant

' Given a source file name (path to the GIF or JPG on disk), return an array containing

' the width (1st element) and height (2nd element).

Dim retVal As Variant

Dim header As String

Dim f As Integer

Dim wHi As Variant

Dim wLo As Variant

Dim hHi As Variant

Dim hLo As Variant

Dim w As Integer ' width of image

Dim h As Integer ' height of image

Dim foundMarker As Integer

Redim retVal(2) As Integer

Redim retVal(Lbound(retVal)+1) ' Size it so there's 2 entries

retVal(Lbound(retVal)) = 0

retVal(Ubound(retVal)) = 0

f = Freefile()

On Error Resume Next

Open fileName For Input As #f

On Error Goto 0

If Err <> 0 Then

ImageSize = retVal ' File name incorrect - return zero for both the height and width

Exit Function

End If

If Lcase(Right(fileName, 3)) = "gif" Then

' GIF's height and width stored in a fixed location

header = Input(10, f)

wHi = Mid(header, 8, 1)

wLo = Mid(header, 7, 1)

hHi = Mid(header, 10, 1)

hLo = Mid(header, 9, 1)

w = Asc(wHi) * 256 + Asc(wLo)

h = Asc(hHi) * 256 + Asc(hLo)

Elseif Lcase(Right(fileName, 3)) = "jpg" Then

' JPG's stored in a variable location. The code has been verified with JFIF

' file format (the most common format)

On Error Goto EndOfFile ' In case we run over the file for some reason

header = Input(2, f)

If header = Chr$(255) & Chr$(216) Then ' Must start with hex FF D8

foundMarker = False ' Look for the marker that will contain the height and width

While Not foundMarker

header = Input(2, f) ' Grab the next marker

' Look for the marker (in hex) FF C0, FF C1, FF C2, or FF C3

If header = Chr$(255) & Chr$(192) Or header = Chr$(255) & Chr$(193) _

Or header = Chr$(255) & Chr$(194) Or header = Chr$(255) & Chr$(195) Then

' Next two bytes are the length, then a single byte that can be ignored.

header = Input(3, f)

' Next two bytes are the height of the image

header = Input(2, f)

hHi = Asc(Midbp(header, 1, 1))

hLo = Asc(Midbp(header, 2, 1))

h = hHi * 256 + hLo

' Next two bytes are the width of the image

header = Input(2, f)

wHi = Asc(Midbp(header, 1, 1))

wLo = Asc(Midbp(header, 2, 1))

w = wHi * 256 + wLo

foundMarker = True ' Exit the while loop

Else ' It's not one of the special markers - skip over it

header = Input(2, f) ' Next two bytes are the marker length

wHi = Asc(Midbp(header, 1, 1))

wLo = Asc(Midbp(header, 2, 1))

w = wHi * 256 + wLo

header = Input(w-2, f) ' Skip over that many bytes (minus the 2 byte length already read)

w = 0 ' Clear the variable

End If

Wend ' Continue until the marker is found

End If ' Ends the check to see if the file starts with FF D8

EndOfFile:

If Err <> 0 Then

Err = 0

Resume AfterError

End If

End If ' Ends the check to see if the format is GIF or JPG

AfterError:

retVal(Lbound(retVal)) = w

retVal(Ubound(retVal)) = h

Close #f

ImageSize = retVal

End Function

Here's a sample GIFFile class cloning ImageSize() routine original logic:

Private Const GIF_HEADER_LENGTH = 10

Private Const GIF_MARKER = "GIF"

Private Const GIF_ID1 = "87a"

Private Const GIF_ID2= "89a"

Private Class GIFFile

Private m_w As Integer

Private m_h As Integer

Public Property Set fileName As String

Dim h ' GIF file Header: "GIF87a" or GIF89a" followed by logical width & height

h = Me.Header ' Let's check GIF format presence..

If ( Left$( h, 3 ) <> GIF_MARKER ) Then Error 1000, _

|Not a GIF file: Graphical Interchange File "GIF" marker not found|

If ( Mid$( h, 4, 3 ) <> GIF_ID1 And Mid$( h, 4, 3 ) <> GIF_ID2 ) Then Error 1002, _

|Not a GIF file: Graphical Interchange File "87a/89a" identifier not found|

m_w = Asc( Mid( h, 8, 1 ) ) * 256 + Asc( Mid( h, 7, 1 ) ) ' Little-endian Screen Width

m_h = Asc( Mid( h, 10, 1 ) ) * 256 + Asc( Mid( h, 9, 1 ) ) ' Little-endian Screen Height

End Property

Private Property Get Header As Variant

Dim h As Integer

h% = Freefile()

Open Me.Name For Input Shared As #h

Header = Input( GIF_HEADER_LENGTH, #h )

Close #h

End Property

Public Property Get Heigth As Integer

Heigth = m_h

End Property

Public Property Get Width As Integer

Me.Width = m_w

End Property

Public Sub new( fileName As String )

Me.FileName = fileName

End Sub

End Class

I have added GIF format additional checks intended to detect files holding inaccurate extension/type

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有