Announcement

Collapse
No announcement yet.

How to get the pixel width and height of a Bitmap file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get the pixel width and height of a Bitmap file

    According to https://en.wikipedia.org/wiki/BMP_file_format

    the Bitmap file's pixel width and height can be obtainable from

    Offset (hex) = 12 and 14 of the BITMAPCOREHEADER

    How do I get these info ?



  • #2
    From the AfxWin.inc file in my headers:

    Code:
    ' ========================================================================================
    ' Retrieves the width of a bitmap
    ' ========================================================================================
    FUNCTION AfxGetBitmapWidth (BYVAL hBitmap AS DWORD) AS LONG
       LOCAL bm AS BITMAP
       IF hBitmap = 0 THEN EXIT FUNCTION
       IF GetObject(hBitmap, SIZEOF(bm), bm) THEN FUNCTION = bm.bmWidth
    END FUNCTION
    ' ========================================================================================
    
    ' ========================================================================================
    ' Retrieves the height of a bitmap
    ' ========================================================================================
    FUNCTION AfxGetBitmapHeight (BYVAL hBitmap AS DWORD) AS LONG
       LOCAL bm AS BITMAP
       IF hBitmap = 0 THEN EXIT FUNCTION
       IF GetObject(hBitmap, SIZEOF(bm), bm) THEN FUNCTION = bm.bmHeight
    END FUNCTION
    ' ========================================================================================
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      Code:
      logotype_foretaget:
          Filnr = freefile
          open "flogo.bmp" for binary as filnr
          get #filnr, 19, bmp_width
          get #filnr, 23, bmp_high
          close filnr
      Fim Wästberg

      Comment


      • #4
        Thanks Jose,
        How do I use these functions ?
        Can you please give an example program?

        Comment


        • #5
          Thanks so much Fim

          Comment


          • #6
            Here's the code for bitmap check and its size


            Code:
            #COMPILE EXE
            #DIM ALL
            
            
            FUNCTION PBMAIN () AS LONG
               LOCAL givenFilnam AS STRING
               LOCAL  gbmp_width  , gbmp_high AS LONG
               givenFilnam = "C:\Temp\BMP files\city.bmp"
            
              IF  ChkBmpFile(givenFilnam) = 1 THEN
                  ? givenFilnam + "  is a bitmap file "
                  GitBmpSize(gbmp_width, gbmp_high , givenFilnam)
                  ? "Bitmap Size  " + STR$(gbmp_width)+ " x " + STR$(gbmp_high)
            
              ELSE
                  ? givenFilnam + "  is NOT a bitmap file "
              END IF
            
            
            END FUNCTION
            
            
            '===================================
            SUB GitBmpSize(bmp_width AS LONG , bmp_high AS LONG,bmpfnam AS STRING )
                LOCAL Filnr AS LONG
                Filnr = FREEFILE
                OPEN bmpfnam FOR BINARY AS filnr
                GET #filnr, 19, bmp_width
                GET #filnr, 23, bmp_high
                CLOSE filnr
            
             END SUB
            
            
            
            '===================================
            FUNCTION ChkBmpFile(gftnam AS STRING) AS LONG
              ' reads in the first 2 bits of the file as a  string
              ' to verify whether it is a bitmap file
              ' It is a bitmap file if the 2 bits is "BM"
              '  https://en.wikipedia.org/wiki/BMP_file_format
              ' Function will return 1 if it is a bitmap file
              LOCAL fft AS LONG
              LOCAL p2bit AS STRING
            
                IF ISFALSE ISFILE(gftnam) THEN
             '      File not found error
                    FUNCTION = 0
                    EXIT FUNCTION
               END IF
            
             ' Read in the first 2 bits only as this will save
             ' time from reading a massive file
               fft = FREEFILE
               OPEN gftnam FOR BINARY AS #fft
               GET$ #fft, 2, p2bit
               CLOSE #fft
            
             ' check whether it is a bitmap file
              IF p2bit ="BM" THEN
                ' this is a bitmap file
                 FUNCTION = 1
              ELSE
                ' this is NOT a BITMAP file
                 FUNCTION = 0
              END IF
            END  FUNCTION

            Comment


            • #7
              Originally posted by Anne Wilson View Post
              According to https://en.wikipedia.org/wiki/BMP_file_format

              the Bitmap file's pixel width and height can be obtainable from

              Offset (hex) = 12 and 14 of the BITMAPCOREHEADER

              How do I get these info ?

              Open the file as BINARY and read bytes 19-22 and 23-26 as LONGs.
              --
              [URL="http://www.camcopng.com"]CAMCo - Applications Development & ICT Consultancy[/URL][URL="http://www.hostingpng.com"]
              PNG Domain Hosting[/URL]

              Comment


              • #8
                Originally posted by Anne Wilson View Post
                According to https://en.wikipedia.org/wiki/BMP_file_format

                the Bitmap file's pixel width and height can be obtainable from

                Offset (hex) = 12 and 14 of the BITMAPCOREHEADER

                How do I get these info ?

                If you are using the bitmap, you can load it and use GRAPHIC GET CANVAS.
                The world is strange and wonderful.*
                I reserve the right to be horrifically wrong.
                Please maintain a safe following distance.
                *wonderful sold separately.

                Comment


                • #9
                  Offset is h12 for width and h16 for height.(Offsets h12 and h14 are for OS/2 bmps) Note - the offsets are h12 and h16 are for if you opened with BASE = 0.
                  Default is BASE = 1, which would make the offsets for width and height h13 and h17.

                  Use-
                  OPEN blah FOR BINARY blah AS #zz blah BASE = 0 '(to stay with numbers on Wikipedia page)
                  GET [#] filenum&, &h12&[or &h13&], X_long 'leading "&h" indicates it is hex
                  GET [#] filenum&, &h16&[or &h17&], Y_Long 'trailing "&" forces LONG type
                  'in square brackets is BASE=1 values
                  (which is close to what Stuart said, except he used default base (BASE = 1) and decimal literals)

                  added - RecPos could be quad ("&&")
                  Last edited by Dale Yarker; 15 Nov 2017, 07:44 AM.
                  Dale

                  Comment


                  • #10
                    If you are really getting into bitmaps, these C structs may help
                    Code:
                    typedef struct                       /**** BMP file header structure ****/
                        {
                        unsigned short bfType;           /* Magic number for file */
                        unsigned int   bfSize;           /* Size of file */
                        unsigned short bfReserved1;      /* Reserved */
                        unsigned short bfReserved2;      /* ... */
                        unsigned int   bfOffBits;        /* Offset to bitmap data */
                        } BITMAPFILEHEADER;
                    
                    typedef struct                       /**** BMP file info structure ****/
                        {
                        unsigned int   biSize;           /* Size of info header */
                        int            biWidth;          /* Width of image */
                        int            biHeight;         /* Height of image */
                        unsigned short biPlanes;         /* Number of color planes */
                        unsigned short biBitCount;       /* Number of bits per pixel */
                        unsigned int   biCompression;    /* Type of compression to use */
                        unsigned int   biSizeImage;      /* Size of image data */
                        int            biXPelsPerMeter;  /* X pixels per meter */
                        int            biYPelsPerMeter;  /* Y pixels per meter */
                        unsigned int   biClrUsed;        /* Number of colors used */
                        unsigned int   biClrImportant;   /* Number of important colors */
                        } BITMAPINFOHEADER;
                    The world is strange and wonderful.*
                    I reserve the right to be horrifically wrong.
                    Please maintain a safe following distance.
                    *wonderful sold separately.

                    Comment


                    • #11
                      Thanks so much guys, my post in #6 already got this resolve

                      Comment

                      Working...
                      X