Obtain the actual size of the JPG file

Source: Internet
Author: User

Obtain the actual size of the JPG file bask

Compile it into a component and use it. PNG is also supported.

'I have released this source code into the public domain. You may use it
'With no strings attached.
'Just call getimagesize with a string containing the filename, and
'It will return a user defined type 'imagesize' (see below)
'Return values of 0 indicate an error of some sort. The error handling
'In this module is limited. There is * No * error handling on the test
'Form. This routine is limited to X or Y sizes of 32767 pixels, but that
'Could not be a problem.

'Check back at http://www.qtm.net /~ Davidc
'I may add support for more file types.

'Supported in this version:
'Jpeg
'Gif
'Png

'This routine does not require any royalty fees for Unisys as it
'Does nothing with the compressed part of GIF files. It simply reads
'4 bytes to determine image size.

Option explicit
Public wimg as long
Public himg as long
Public type imagesize
Width as long
Height as long
End type

Public sub getimagesize (sfilename as string)
On Error resume next 'you' ll want to change this
Dim FN as integer
Dim btemp (3) as byte
Dim lflen as long
Dim LPOS as long
Dim bhmsb as byte
Dim bhlsb as byte
Dim bwmsb as byte
Dim bwlsb as byte
Dim bbuf (7) as byte
Dim bdone as byte
Dim icount as integer

Lflen = filelen (sfilename)
Fn = freefile
Open sfilename for binary as FN
Get # interferon, 1, btemp ()

'Png File
If btemp (0) = & H89 and btemp (1) = & h50 and btemp (2) = & h4e _
And btemp (3) = & h47 then
Get # interferon, 19, bwmsb
Get # interferon, 20, bwlsb
Get # interferon, 23, bhmsb
Get # interferon, 24, bhlsb
'Getimagesize. width = combinebytes (bwlsb, bwmsb)
'Getimagesize. Height = combinebytes (bhlsb, bhmsb)
Wimg = combinebytes (bwlsb, bwmsb)
Himg = combinebytes (bhlsb, bhmsb)
End if

'Gif File
If btemp (0) = & h47 and btemp (1) = & h49 and btemp (2) = & h46 _
And btemp (3) = & H38 then
Get # interferon, 7, bwlsb
Get # interferon, 8, bwmsb
Get # interferon, 9, bhlsb
Get # interferon, 10, bhmsb
'Getimagesize. width = combinebytes (bwlsb, bwmsb)
'Getimagesize. Height = combinebytes (bhlsb, bhmsb)
Wimg = combinebytes (bwlsb, bwmsb)
Himg = combinebytes (bhlsb, bhmsb)
End if


'Jpeg File
If btemp (0) = & HFF and btemp (1) = & hd8 and btemp (2) = & HFF then
Debug. Print "Jpeg"
LPOS = 3
Do
Do
Get # interferon, LPOS, bbuf (1)
Get # interferon, LPOS + 1, bbuf (2)
LPOS = LPOS + 1
Loop until (bbuf (1) = & HFF and bbuf (2) <> & HFF) or LPOS> lflen

For icount = 0 to 7
Get # interferon, LPOS + icount, bbuf (icount)
Next icount
If bbuf (0) >=& hc0 and bbuf (0) <= & hc3 then
Bhmsb = bbuf (4)
Bhlsb = bbuf (5)
Bwmsb = bbuf (6)
Bwlsb = bbuf (7)
Bdone = 1
Else
LPOS = LPOS + (combinebytes (bbuf (2), bbuf (1) + 1
End if
Loop while LPOS <lflen and bdone = 0
'Getimagesize. width = combinebytes (bwlsb, bwmsb)
'Getimagesize. Height = combinebytes (bhlsb, bhmsb)
Wimg = combinebytes (bwlsb, bwmsb)
Himg = combinebytes (bhlsb, bhmsb)
End if
Close interferon

End sub
Private function combinebytes (LSB as byte, MSB as byte) as long
Combinebytes = clng (LSB + (MSB * 256 ))
End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.