如何获得jpg文件的实际尺寸呢?
来源:岁月联盟
时间:2003-07-11
<br>
<br>
'I have released this source code into the public domain. You may use it<br>
'with no strings attached.<br>
'Just call GetImageSize with a string containing the filename, and<br>
'it will return a user defined type 'ImageSize' (see below)<br>
'Return values of 0 indicate an error of some sort. The error handling<br>
'in this module is limited. There is *NO* error handling on the test<br>
'form. This routine is limited to X or Y sizes of 32767 pixels, but that<br>
'should not be a problem.<br>
<br>
'Check back at http://www.qtm.net/~davidc<br>
'I may add support for more file types.<br>
<br>
'supported in this version:<br>
'JPEG<br>
'GIF<br>
'PNG<br>
<br>
'This routine does not require any royalty fees for Unisys as it<br>
'does nothing with the compressed part of GIF files. It simply reads<br>
'4 bytes to determine image size.<br>
<br>
Option Explicit<br>
Public WImg As Long<br>
Public HImg As Long<br>
Public Type ImageSize<br>
Width As Long<br>
Height As Long<br>
End Type<br>
<br>
Public Sub GetImageSize(sFileName As String)<br>
On Error Resume Next 'you'll want to change this<br>
Dim iFN As Integer<br>
Dim bTemp(3) As Byte<br>
Dim lFlen As Long<br>
Dim lPos As Long<br>
Dim bHmsb As Byte<br>
Dim bHlsb As Byte<br>
Dim bWmsb As Byte<br>
Dim bWlsb As Byte<br>
Dim bBuf(7) As Byte<br>
Dim bDone As Byte<br>
Dim iCount As Integer<br>
<br>
lFlen = FileLen(sFileName)<br>
iFN = FreeFile<br>
Open sFileName For Binary As iFN<br>
Get #iFN, 1, bTemp()<br>
<br>
'PNG file<br>
If bTemp(0) = &H89 And bTemp(1) = &H50 And bTemp(2) = &H4E _<br>
And bTemp(3) = &H47 Then<br>
Get #iFN, 19, bWmsb<br>
Get #iFN, 20, bWlsb<br>
Get #iFN, 23, bHmsb<br>
Get #iFN, 24, bHlsb<br>
'GetImageSize.Width = CombineBytes(bWlsb, bWmsb)<br>
'GetImageSize.Height = CombineBytes(bHlsb, bHmsb)<br>
WImg = CombineBytes(bWlsb, bWmsb)<br>
HImg = CombineBytes(bHlsb, bHmsb)<br>
End If<br>
<br>
'GIF file<br>
If bTemp(0) = &H47 And bTemp(1) = &H49 And bTemp(2) = &H46 _<br>
And bTemp(3) = &H38 Then<br>
Get #iFN, 7, bWlsb<br>
Get #iFN, 8, bWmsb<br>
Get #iFN, 9, bHlsb<br>
Get #iFN, 10, bHmsb<br>
'GetImageSize.Width = CombineBytes(bWlsb, bWmsb)<br>
'GetImageSize.Height = CombineBytes(bHlsb, bHmsb)<br>
WImg = CombineBytes(bWlsb, bWmsb)<br>
HImg = CombineBytes(bHlsb, bHmsb)<br>
End If<br>
<br>
<br>
'JPEG file<br>
If bTemp(0) = &HFF And bTemp(1) = &HD8 And bTemp(2) = &HFF Then<br>
Debug.Print "JPEG"<br>
lPos = 3<br>
Do<br>
Do<br>
Get #iFN, lPos, bBuf(1)<br>
Get #iFN, lPos + 1, bBuf(2)<br>
lPos = lPos + 1<br>
Loop Until (bBuf(1) = &HFF And bBuf(2) <> &HFF) Or lPos > lFlen<br>
<br>
For iCount = 0 To 7<br>
Get #iFN, lPos + iCount, bBuf(iCount)<br>
Next iCount<br>
If bBuf(0) >= &HC0 And bBuf(0) <= &HC3 Then<br>
bHmsb = bBuf(4)<br>
bHlsb = bBuf(5)<br>
bWmsb = bBuf(6)<br>
bWlsb = bBuf(7)<br>
bDone = 1<br>
Else<br>
lPos = lPos + (CombineBytes(bBuf(2), bBuf(1))) + 1<br>
End If<br>
Loop While lPos < lFlen And bDone = 0<br>
'GetImageSize.Width = CombineBytes(bWlsb, bWmsb)<br>
'GetImageSize.Height = CombineBytes(bHlsb, bHmsb)<br>
WImg = CombineBytes(bWlsb, bWmsb)<br>
HImg = CombineBytes(bHlsb, bHmsb)<br>
End If<br>
Close iFN<br>
<br>
End Sub<br>
Private Function CombineBytes(lsb As Byte, msb As Byte) As Long<br>
CombineBytes = CLng(lsb + (msb * 256))<br>
End Function











