BMP Tutor - Read *.bmp (resource) --> hBMP (LoadImage)

Category: BMP Tutor Series (API)

Date: 02-16-2022

Return to Index


 
'This snippet demonstrates working with the LoadImage() API to load a
'bitmap and place it on a display device context.
 
'When a bitmap is loaded with the LoadImage() API, the following data
'structure is created. It is NOT the same structure as is contained
'in a *.bmp file.
 
'A bitmap file contains a DIB, whereas the LoadImage() API creates a
'DDB bitmap - a bitmap compatible with the display.
 
   Type Bitmap
     bmType As Long           'must be zero
     bmWidth As Long          'width (pixels)
     bmHeight As Long         'height (pixels)
     bmWidthBytes As Long     'bytes per scan line (must be divisible by 2)
     bmPlanes AS WORD         'count of color plans (1 for 24/32bit bitmaps)
     bmBitsPixel AS WORD      '24/32 bots per pixel
     bmBits AS BYTE PTR       'pointer to byte array (color data)
   End Type
 
'The format of the bmBits array is usually not known, nor available, to a
'programmer. However, the color data can be retrieved using the GetDIBits API,
'edited by an application, and then returned to the display device using the
'SetDIBits API.
 
 
'Primary Code:
'To load and display a bitmap file, the following API called is used:
 
   hBmp = LoadImage ( %NULL, fSource, %IMAGE_BITMAP, 0, 0, %LR_LOADFROMFILE )
 
'Once the handle (hBMP) to the bitmap is available, it may be selected into
'a compatible DC and modified using the GDI API.
 
 
'Compilable Example:  (Jose Includes)
'This reads and displays a bitmap from a resource embedded within the EXE.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource "gbsnippets.pbr"
Global hDlg, hGraphic, hBMP, DC_graphic, DC_BMP as DWord
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, 300,"Load From Resource", 20,10,150,20
   Control Add Image, hDlg, 200,"", 50,50,100,100
   Control Handle hDlg, 200 To hGraphic
   DC_graphic = GetDC (hGraphic)                           'DC for Image control
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   If CB.Msg = %WM_Command AND CB.Ctl = 300 AND CB.Ctlmsg = %BN_Clicked Then
      LoadBitMapResourceThenDisplay ("cowgirl", 0)
   End If
End Function
 
Sub LoadBitMapResourceThenDisplay (fSource As Asciiz*%Max_Path, iSource&)
   'load bitmap from resource
   hBmp = LoadImage ( GetModuleHandle(""), fSource, %IMAGE_BITMAP, 0, 0, %LR_DEFAULTCOLOR )  'imageid identifier of resource
 
   'create compatible DC, select the hBMP into it and modify with GDI API
   DC_BMP = CreateCompatibleDC ( %NULL )   'create DC compatible with the screen (to hold bitmap)
   SelectObject ( DC_BMP, hBmp )           'sSelect bitmap into DC, making it available for use with API
 
   'use GetObject to get w/h, needed for BitBlt to copy image into control DC
   Local bm As Bitmap                      'BITMAP structure contains image w/h, which is needed with BibBlt API
   GetObject ( hBmp, SizeOf(bm), bm )      'bitmap info loaded into bm, w/h are in bm.bmWidth AND bm.bmHeight
   BitBlt ( DC_graphic, 0, 0, bm.bmWidth, bm.bmHeight, DC_BMP, 0, 0, %SRCCOPY )    'hDC_BMP is blitted to visible DC hDC_graphic
End Sub
 
'gbs_00521
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm