Announcement

Collapse
No announcement yet.

Displaying bitmap from resource file

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

  • Displaying bitmap from resource file

    The bitmap included in a resource file is not displayed by the code. The first few line of the RC file are:

    #include "c:\pbcc40\winapi\resource.h"

    // Windows 95 does NOT display icons FOR CONSOLE applications IN Explorer.
    RADTABS ICON MIRD.ICO
    MESSAGE BITMAP PATIENT.BMP
    .....

    And the following code fragment fails to display the bitmap:

    DESKTOP GET CLIENT TO ncWidth&, ncHeight&
    BitName$ = "message"
    nFile = FREEFILE
    OPEN BitName$ FOR BINARY AS nFile
    GET #nFile, 19, nWidth&
    GET #nFile, 23, nHeight&
    CLOSE nFile
    GRAPHIC WINDOW "", 20, 20, nWidth&, nHeight& TO hGW1&
    GRAPHIC ATTACH hGW1&, 0, REDRAW
    GRAPHIC RENDER BitName$, (0, 0) - (nWidth&-1, nHeight&-1)
    GRAPHIC SET LOC (ncWidth& - nWidth&)\2, (ncHeight& - nHeight&)\2
    GRAPHIC REDRAW
    GRAPHIC SET FOCUS

    The bitmap is display if BitName$ = "patient.bmp". What am I doing wrong?

  • #2
    You are not exactly trying to load from the resource, but instead also using the file. Comment out the lines:
    Code:
    OPEN BitName$ FOR BINARY AS nFile
    GET #nFile, 19, nWidth&
    GET #nFile, 23, nHeight&
    CLOSE nFile
    You need to use the API to help with setting your window, so here's the code that should do the trick.

    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "Win32api.inc"
    #RESOURCE "your_resource_filename.pbr"
    
    FUNCTION PBMAIN () AS LONG
        LOCAL ncWidth&, ncHeight&,nWidth&, nHeight&,BitName$
        LOCAL nFile AS LONG
        LOCAL hGW1 AS DWORD
        LOCAL hInst AS DWORD ,lRes AS DWORD, lRes2 AS DWORD, lRes3 AS DWORD
        hInst = GetModuleHandle("")                             'need this to grab from resource
        lRes = FindResource(hInst,"MESSAGE",BYVAL %RT_BITMAP)
        IF lRes  THEN lRes2 = LoadResource(hInst, lRes)          'if found, load the BMP
        IF lRes2 THEN lRes3 = LockResource(lRes2)                'lock to get a pointer to the BMP file'
        IF lRes3 THEN
            nWidth& = PEEK(LONG,lRes3 + 4) 'get x and y
            nHeight& = PEEK(LONG,lRes3 + 8)
            ? FORMAT$(nWidth&) + $CRLF +FORMAT$(nHeight&)
        ELSE
            ? "Error - no bitmap in the resource"
            EXIT FUNCTION
        END IF
    
        DESKTOP GET CLIENT TO ncWidth&, ncHeight&
        BitName$ = "MESSAGE"
        nFile = FREEFILE
    '    OPEN BitName$ FOR BINARY AS nFile
    '    GET #nFile, 19, nWidth&
    '    GET #nFile, 23, nHeight&
    '    CLOSE nFile
        GRAPHIC WINDOW "", 20, 20, nWidth&, nHeight& TO hGW1
        GRAPHIC ATTACH hGW1, 0, REDRAW
        GRAPHIC CLEAR %WHITE,%WHITE
        GRAPHIC RENDER BitName$, (0, 0) - (nWidth&-1, nHeight&-1)
        GRAPHIC SET LOC (ncWidth& - nWidth&)\2, (ncHeight& - nHeight&)\2
        GRAPHIC REDRAW
        GRAPHIC SET FOCUS
        GRAPHIC REDRAW
    
        'SLEEP 5000
        
        WAITKEY$
    
        GRAPHIC BITMAP END
       
    END FUNCTION
    I've tested this with my own resource, different bitmap name, but it works.
    Last edited by Richard Angell; 24 Jan 2008, 04:22 PM.
    Rick Angell

    Comment


    • #3
      You do not open the file if you want to load the bitmap from the resource. (The file is no longer part of your application)

      Also, those names ARE case-sensitve. ("MESSAGE" is not the same as "message")

      I think you want something like

      GRAPHIC BITMAP LOAD "MESSAGE" ....

      I don't know how you'd get the dimensions from a resource. ( Well, I know how I would do it: LoadImage() + GetObject() then read the BITMAPHEADER structure) but for now I think you just need to get your "base" code working).

      Why the help has that info about reading a BMP file on the same page with GRAPHIC BITMAP LOAD is a mystery. That just confuses the issue.

      Oh... why does it work when you specify the complete file name? Because that's what the help file says it will do.


      MCM
      Michael Mattias
      Tal Systems Inc. (retired)
      Racine WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Thanks Rick and Michael. Rick's response cleared up the issue. I knew why it worked as a file (per PBCC's help file) but need help (an example) on how to get the file from the resource. Thanks guys.
        Last edited by Keith Eckerman; 24 Jan 2008, 04:33 PM.

        Comment


        • #5
          Michael's comments give some good points too,especially on the resource name being "case-sensitve".

          The code to get the resource for the purpose of getting the dimensions, is modeled on some code that Borje Hagsten had posted some years back. It has a variety of uses as the basic API calls will get the resource loaded and locked so you can do what needs be done with it. For example, more so in PBWin, it is useful to load a .rtf help file embedded in the resource, copy to a string and load a rich text control. There are examples in some of the other forums showing how to do this.
          Rick Angell

          Comment


          • #6
            Just to make this a little clearer...BITMAP resources are stored as a BITMAP structure followed by more data...

            Code:
            typedef struct tagBITMAP {
              LONG   bmType; 
              LONG   bmWidth; 
              LONG   bmHeight; 
              LONG   bmWidthBytes; 
              WORD   bmPlanes; 
              WORD   bmBitsPixel; 
              LPVOID bmBits; 
            } BITMAP, *PBITMAP;
            I can't find TagBitmap structure in Win32API, but we can make one...or at least as much as we need...
            Code:
            TYPE tagBitMap
               bmType   AS LONG
               bmWidth  AS LONG
               bmHeight AS LONG
            END TYPE
            (We only need this much, you'll see where we are going)


            Now let's ...
            A) get rid of the PEEK with the inline numeric literals ("magic numbers")
            .... and at the same time ...
            B) Why don't we make this a procedure we can re-use?

            Code:
            FUNCTION GetResourceBitMapSize (szBM AS ASCIIZ, nWidth AS LONG, nHeight AS LONG)  AS LONG
            
              LOCAL pBM AS tagBitMap PTR, hInst AS LONG, lres as LONG, lres2 AS LONG
            
                FUNCTION = %FALSE   ' default = fail
            
                hInst    = GetModuleHandle   ("")                        'resource in this module
                lRes     = FindResource(hInst,szBM ,BYVAL %RT_BITMAP)
                IF lRes  THEN lRes2 = LoadResource(hInst, lRes)          'if found, load the BMP
                IF lRes2 THEN pBM = LockResource(lRes2)                'lock to get a pointer to the BMP file'
                IF pBM  THEN 
                    nWidth    = @pBM.BmWidth
                    nHeight   = @pBM.bmHeight
                    FUNCTION  =  %TRUE         ' SUCCESS!!
                END IF
            
            END FUNCTION
            
            ..
               szBM = "MESSAGE"
            
               IF ISTRUE GetResourceBitmapSize (szBm, nWidth, nHEight) THEN
                  MSGBOX USING$ ("BitMap '&' is # pixels wide x # pixels High", szBM, nWidth, nHeight)
               ELSE
                  MSBGOX USING$ ("Bitmap resource '&' not found or corrupt", szBM)
               END IF
            MCM
            Last edited by Michael Mattias; 24 Jan 2008, 05:52 PM.
            Michael Mattias
            Tal Systems Inc. (retired)
            Racine WI USA
            [email protected]
            http://www.talsystems.com

            Comment

            Working...
            X