can't see the joint but if I ask bmp that the only thing show.
( I try gif or jpeg in the PB forum and FF forum with no real result )
Code:
nFile& = FREEFILE OPEN "myimage.bmp" FOR BINARY AS nFile& GET #nFile&, 19, nWidth& GET #nFile&, 23, nHeight& CLOSE nFile&
First, let's look at the way bitmap files are stored on disk:
Bitmaps should be saved in a file that uses the established bitmap file format and assigned a name with the three-character .bmp extension. The established bitmap file format consists of a BITMAPFILEHEADER structure followed by a BITMAPINFOHEADER, BITMAPV4HEADER, or BITMAPV5HEADER structure
Code:
UNION BITMAPUNION BMIH AS BITMAPINFOHEADER BMV4H AS BITMAPV4HEADER BMV5H As BITMAPV5HEADER END UNION TYPE BITMAPSTARTOFFILE BMFH AS BITMAPFILEHEADER ' size = 14 BMU AS BITMAPUNION END TYPE
But that is a lot more complex than we need for these purposes.
When you inspect the three UNION members, you find in all cases the width and height of the bitmap are located at offsets 4 and 8 respectively of the BITMAPINFOHEADER, BITMAPv4HEADER or BITMAPV5HEADER; which, when you add the 14 bytes for the BITMAPFILEHEADER, puts the width and height at offsets 18 and 22.. which correspond to 'positions' 19 and 23 (the 'magic numbers!') when the file is opened for BINARY with the default BASE of 1.
Since we don't really need all the info from the UNION, we can simplify the type we use for the start of the file...
Code:
TYPE BITMAPSTARTOFFILE BMIH AS BITMAPINFOHEADER Unused AS LONG ' actually structure size, but we don't need that. nWidth AS LONG nHeight AS LONG END TYPE
Code:
LOCAL BMSOF AS BITMAPSTARTOFILE ' the simple version above nFile& = FREEFILE OPEN "myimage.bmp" FOR BINARY AS nFile& GEt #nFile, , BMSOF ' read the data at start of file CLOSE nFile& ' done with file, close it ' get our values from the file data in BMSOF: nWidth& = BMSOF.nWidth nHeight& = BMSOF.nHeight
FWIw, I think I'd create a "Write once, use many" FUNCTION or SUB for this
Code:
FUNCTION GetBitmapWidthandHeightFromFile (szBmpFile AS ASIICZ, nWidth as Long, nHeight As LONG) AS LONG LOCAL BMSOF AS BITMAPSTARTOFILE ' the simple version above LOCAL nFile AS LONG, fv AS LONG ON ERROR RESUME NEXT nFile = FREEFILE OPEN "myimage.bmp" FOR BINARY AS nFile& IF ISFALSE ERR THEN GET #nFile, , BMSOF ' read the data at start of file CLOSE nFile ' done with file, close it nWidth = BMSOF.nWidth nHeight = BMSOF.nHeight fv = 0 ' success! ELSE fv = ERRCLEAR ' assign and reset END IF FUNCTION = fv END FUNCTION CALL GetBitmapWidthAndHeightFromFile ("myimage.bmp", nWidth, nHeight) to lresult IF ISFALSE lresult THEN we got the width and height OK, do cool stuff ELSE MSGBOX USING$("Error getting Bitmap width and height # &", lResult, ERROR$(lresult)), %MB_ICONHAND, "Bitmap file access error" .....
Leave a comment: