Announcement

Collapse
No announcement yet.

How to detect whether a given file is a bmp file

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

  • How to detect whether a given file is a bmp file

    I wish to use Powerbasic to check the file format of a bmp file before opening it.
    Is there a way to check for valid bmp file format and if it isn't a bmp file display an error message that this is not a bmp file.





  • #2
    Are the first two bytes "BM"? File name end with dot BMP (upper or lower case)? Then yes.
    https://en.wikipedia.org/wiki/BMP_file_format shows where size and palette are.
    Dale

    Comment


    • #3
      Thanks so much Dale

      I did a small program which works on your idea

      Code:
      #COMPILE EXE
      #DIM ALL
      
      
      FUNCTION PBMAIN () AS LONG
         LOCAL givenFilnam AS STRING
         givenFilnam = "C:\Temp\BMP files\coffee.bmp"
      
        IF  ChkBmpFile(givenFilnam) = 1 THEN
            ? givenFilnam + "  is a bitmap file "
        ELSE
            ? givenFilnam + "  is NOT a bitmap file "
        END IF
      
      END FUNCTION
      
      
      
      
      '===================================
      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
        IF p2bit ="BM" THEN
           FUNCTION = 1
        ELSE
           FUNCTION = 0
        END IF
      END  FUNCTION

      Comment


      • #4
        I'd just call (WindAPI) LoadImage() or maybe even (WinAPI) LoadBitmap() or maybe even (Powerbasic) BITMAP LOAD.

        If it fails, the file is not a BMP. Or, even if it is a BMP, you won't be able to do anything with it anyway since you can't load it so it may as well be an "invalid format."
        Michael Mattias
        Tal Systems Inc.
        Racine WI USA
        mmattias@talsystems.com
        http://www.talsystems.com

        Comment

        Working...
        X