Calculate Height and Width of GIFJPG Files

xiaoxiao2021-03-06  60

This is a reprint 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 = 0 RetVal (Ubound (Retval)) = 0 f = freefile () on Error ResMe Next Open FileName for Input AS #f on error goto 0 if err <> 0 the imagesize = return 'File Name IncorRect - Right (FileName, 3)) = "GIF" THEN GIF' S Height and width store = input (10, f) whi = mid (header, 8, 1) w LO = 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) TEN '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 That Can Be ionore. Header = Input (3, f)' Next Two Bytes Are The Height of the Image HEADES Are The Height of the Image HEADER = INPUT (2, F) HHI = ASC (HEADER, 1, 1)) HLO = ASC (HEADBP (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 D8ndoffile: IF Err <

> 0 THEN ERR = 0 Resume AfterError End if End If 'Ends The Check To See if' Ends The Check To See if 'Ends The CHECK TO See If' Ends The CHECK TOGAFTERROR: RETVAL (RetVal) = W RetVal (Ubound (Retval) = h close #f imagesize = retvalend FunctionHere's a sample gIFFile class cloning ImageSize () routine original logic: Private Const GIF_HEADER_LENGTH = 10Private Const GIF_MARKER = "GIF" Private Const GIF_ID1 = "87a" Private Const GIF_ID2 = "89a" Private class GIFFilePrivate m_w As IntegerPrivate m_h As IntegerPublic Property Set fileName AS STRINGDIM H 'GIF FILE Header: "GIF87A" or gif89a "FOLLOWED by Logical Width & heighth = me.header' let's check gif format presence..IF (Left $ (h, 3) <> gif_marker) Then Error 1000, _ | NOT A GIF FILE: GRAPHICAL INTERCHANGE "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 widthm_h = ASC (MID (H, 10, 1)) * 256 ASC (MID (H, 9, 1))' Little-Endian Screen Heightend PropertyPrivate Property Get Header AS Variantdim H AS INTEGERH% = Freefile () Open Me.Name For Input Shared As #hHeader = Input (GIF_HEADER_LENGTH, #h) Close #hEnd PropertyPublic Property Get Heigth As IntegerHeigth = m_hEnd PropertyPublic Property Get Width As IntegerMe.Width = m_wEnd PropertyPublic Sub new (fileName As String ) Me.filename =

转载请注明原文地址:https://www.9cbs.com/read-107259.html

New Post(0)