Date: 02-16-2022
Return to Index
created by gbSnippets
'Keep in mind that there are bitmap files (*.bmp) and there are
'bitmaps in memory. The two have similarites, but are NOT the
'same thing. This tutorial covers memory bitmaps.
'A memory bitmap can be created in several wasy:
' - loaded from a resource
' - loaded from a file
' - created within the application
'Windows allows an application to work with memory bitmaps in two ways -
' - operating on a DC to which the memory bitmap has been selected
' - modifying the memory bitmap data structures directly with code
'GDI functions typically operate on a DC, modifying the underlying
'bitmap. Applications can call either the GDI or modify the bitmap data
'structures directly.
'Types of Memory Bitmaps ==================================================
'Bitmaps come in two flavors - DIBs and DDBs.
' - DDB device dependent bitmap
' - DIB device independent bitmap
'DDB bitmaps are called GDI Or compatible bitmaps, and may be selected
'into a DC.
'A DIB bitmap cannot be selected into a DC. It has no handle so it
'cannot be used in most API. There are, however, a few DIB-specific API
'which can copy areas from a DIB onto a DC.
'Color Values ===========================================================
'A DDB does not contain color values. A DIB contains either palette indexes
'or color values.
'Color is defined as a combination of 3 primary colors - red, green, and blue.
'To specificy an explicite RGB color, COLORREF value has the following hexadecimal form:
0x00bbggrr
'Bytes right to left are red, green, blue. The high-order byte must be zero.
'Color data structures of interest
Type RGBTRIPLE Type RGBQUAD
rgbtBlue AS BYTE rgbBlue AS BYTE
rgbtGreen AS BYTE rgbGreen AS BYTE
rgbtRed AS BYTE rgbRed AS BYTE
End Type rgbReserved AS BYTE
End Type
'Macros of value
iColor = Rgb(r,g,b)
R = GetRValue (iColor)
G = GetGValue (iColor)
B = GetBValue (iColor)
'DDB Bitmaps ============================================================
'DDB bitmaps are comprised of a single BITMAP structure:
Type Bitmap
bmType '0
bmWidth 'width (pixels)
bmHeight 'height (pixels)
bmWidthBytes 'bytes per scan line (bit values form a word-aligned array)
'width of array that maps entries from device palette to pixels
bmPlanes 'count of color plans
bmBitsPixel '1=monochrome, 24-true color, 32-alpha channel support
bmBits 'pointer to byte array (word aligned)
End Type
'A DDB does NOT contain color values.
'Monochrome - 1-bit, 1-plane, scanline is 16*n,
' - if bit=1 pixel is set to FG color. if bit=0, pixel is set to background color
'GetDeviceCaps - retrieve color format of a device
'CreateBitmap w, h, Planes, BitsPerPixel, pBYTEarray 'word-aligned array
'CreateBitmapIndirect pBITMAP ---> MSDN suggests use for MONOCHROME
'CreateCompatibleBitmap hDC,w,h ---> MSDN suggests use for COLOR
'SelectObject hDC, hBMP
'DeleteObject hBMP
'DIB Bitmaps ============================================================
BitmapINFO - BitmapINFOHeader 'color, dimensions, compression, ...
- RgbQuad '(2 or more)
Type BitmapInfoHeader Type RGBQUAD
biSize rgbBlue
biWidth rgbGreen
biHeight rgbRed
biPlanes rgbReserved
biBitCount End Type
biCompression
biXPelsPerMeter
biYPelsPerMeter
biClrUsed
biClrImportant
End Type
'A DIB contains color information as color value or color palette indexes.
'Color format - color planes (always 1)
' - color bits 1-2-...-32 per pixel
-
'Transparency ====================================================
'Windows does not support transparent bitmaps, but there are two methods
'programmers can use to create transparency
'1. Using two bitmaps - the desired bitmap and a monochrome MASK
'2. Assign a transparent color value and manually code to not display
' pixels with the transparent color
'MONOCHROME ====================================================
'Use raster operations to create a monochrome bitmap from a color bitmap
'bitmap background default color is white 'SetBkColor API can change
'DC background default is black 'SetTextColor can change
'To transfer bytes from color --> monochrome
' - pixels same color as background color are mapped to the
' background color of the monochrome bitmap. All others are
' mapped as foreground color of the monochrome bitmap
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource "gbsnippets.pbr"
%ID_Graphic = 200
Global hDlg as DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,400,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Load Resource Bitmap", 30,10,140,20
Control Add Graphic, hDlg, %ID_Graphic, "", 30,40,100,100, %WS_Border
Control Add Image, hDlg, 300, "cowgirl", 150,40,100,100, %WS_Border
Graphic Attach hDlg, %ID_Graphic
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case %WM_Command
Select Case CB.Ctl
Case 100
Local hBMP, hDC, hDC_pb as DWord
Graphic Get DC To hDC_pb
hDC = CreateCompatibleDC(hDC_pb)
hBMP = LoadBitmap(GetModuleHandle(""), "cowgirl")
SelectObject(hDC, hBMP)
BitBlt hDC, 10,10,100,100,hDC_pb,0,0,%SRCCopy
End Select
End Select
End Function
'gbs_00496
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm