qwert Posted July 4, 2017 Share Posted July 4, 2017 I need to extract the size of a BMP image without using the usual GDI+ methods. The sizes are clearly present in the first 24 bytes of a BMP header, but I haven't been able to fashion a way to isolate them. Plus, the size characters appear numerically 'swapped'. As an example, the attached result is from a bitmap with a size of 580 x 300. The size is encoded in the right side parameters ... 44020002C01. Would any happen to have a simple Au3 method that can isolate and calculate the two sizes? ... or at least a method to obtain a sequence of 24 true characters. Thanks in advance for any help. Link to comment Share on other sites More sharing options...
Tekk Posted July 5, 2017 Share Posted July 5, 2017 #include <FileConstants.au3> #include <MsgBoxConstants.au3> Local $hFile = FileOpen("Bitmap Image.bmp", $FO_BINARY) Local $bFile = FileRead($hFile, 26) FileClose($hFile) If BinaryToString(BinaryMid($bFile, 1, 2)) == "BM" Then Local $nW = Number(BinaryMid($bFile, 19, 4)) Local $nH = Number(BinaryMid($bFile, 23, 4)) MsgBox($MB_TOPMOST, "Dimensions", $nW & " x " & $nH) Else MsgBox($MB_TOPMOST, "Error!", "Not a bitmap!") EndIf Xandy 1 Link to comment Share on other sites More sharing options...
qwert Posted July 5, 2017 Author Share Posted July 5, 2017 Thanks for that. It's perfect. I haven't worked with BinaryMid() before. I'll look at more examples. Link to comment Share on other sites More sharing options...
Tekk Posted July 5, 2017 Share Posted July 5, 2017 You're welcome. I didn't investigate bitmap headers so it may or may not work on all bitmap images, anyhow, happy to help. Link to comment Share on other sites More sharing options...
qwert Posted July 5, 2017 Author Share Posted July 5, 2017 BMP is all I need this for. But, indeed, there may be other uses. Thanks, again. Link to comment Share on other sites More sharing options...
UEZ Posted July 5, 2017 Share Posted July 5, 2017 (edited) Another way: expandcollapse popup;coded by UEZ build 2017-07-06 #include <WinAPI.au3> #include <WinAPIDiag.au3> Global $sFile = FileOpenDialog("Select a BMP file", "", "Bitmap (*.bmp)") If @error Then Exit Global $tResult = BmpGetHeaderInfo($sFile) ConsoleWrite("width: " & $tResult.width & @CRLF) ConsoleWrite("height: " & $tResult.height & @CRLF) MsgBox(0, "Bitmap Header Information", "File: " & $sFile & @CRLF & "Width: " & $tResult.width & @CRLF & "Height: " & $tResult.height, 30) Func BmpGetHeaderInfo($sBMPFile) If Not FileExists($sBMPFile) Then Return SetError(1, 0, 0) Local Const $tagBMPHeader = "char id[2];align 2;dword size;word unused1;word unused2;dword offset;", _ $tagDIBHeader = "dword dibsize;dword width;dword height;word planes;word pixels;dword compression;dword imgsize;dword hres;dword vres;dword nocol;dword impcol;", _ $tagDIBHeader32 = "dword redchannel;dword greenchannel;dword bluechannel;dword alphachannel;dword colorspace; byte colorspaceendpoints[36];dword rgamma;dword ggamma;dword bgamma;", _ $tagOSHeader = "dword size;word width;word height;word planes;word pixels;" Local $iBytes Local $hFile = _WinAPI_CreateFile($sBMPFile, 2, 2) Local $tDIBHeader = DllStructCreate("dword size") _WinAPI_SetFilePointer($hFile, 0x0E) _WinAPI_ReadFile($hFile, $tDIBHeader, 4, $iBytes) _WinAPI_SetFilePointer($hFile, 0x0) Local $tBMP, $sTag ;~ ConsoleWrite($tDIBHeader.size & @CRLF) Switch $tDIBHeader.size Case 12 ;BITMAPCOREHEADER / OS21XBITMAPHEADER $sTag = "struct;" & $tagBMPHeader & $tagOSHeader & "endstruct" Case 40 ;BITMAPINFOHEADER $sTag = "struct;" & $tagBMPHeader & $tagDIBHeader & "endstruct" Case 124 ;BITMAPV5HEADER $sTag = "struct;" & $tagBMPHeader & $tagDIBHeader & $tagDIBHeader32 & "endstruct" EndSwitch $tBMP = DllStructCreate($sTag) _WinAPI_ReadFile($hFile, $tBMP, 14 + $tDIBHeader.size, $iBytes) _WinAPI_CloseHandle($hFile) ;~ _WinAPI_DisplayStruct($tBMP, $sTag) Return $tBMP EndFunc More information can be found here: https://en.wikipedia.org/wiki/BMP_file_format Edited July 5, 2017 by UEZ Code update Xandy 1 Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
PACaleala Posted July 7, 2017 Share Posted July 7, 2017 (edited) Windows 'knows' the size in pixels for a Bmp: $sFilename = "Ch.BMP" $sFolderPathspec = "L:\" Local $Object = objCreate("Scripting.FileSystemObject") Local $objShell = objCreate("Shell.Application") Local $objFolder = $objShell.Namespace($sFolderPathspec) $strPixsize = $objFolder.GetDetailsOf($objFolder.Parsename($sFileName), 31) msgbox(64,"size in pixels",$sFolderPathspec & $sFilename & @CrLf & $strPixsize,11 ) ; Exit ; ; the script works with a BMP, JPG, PNG, TIF, GIF, ICO file Edited July 10, 2017 by PACaleala Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now