Jump to content

Need way to directly extract BMP's image size


Recommended Posts

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.

 

BMP Header.png

Link to comment
Share on other sites

#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

 

Link to comment
Share on other sites

Another way:

;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 by UEZ
Code update

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

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 by PACaleala
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...