Date: 02-16-2022
Return to Index
created by gbSnippets
'... this snippet is in work
'The image color data in a bitmap file can be easily read
'into a one-dimensional bytes array. And if required, it
'can be converted into a bitstring suitable for use with
'PowerBASIC graphic targets.
'Primary Code:
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg as DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Control Add Graphic, hDlg, 200,"",50,40,100,100, %WS_Border
Graphic Attach hDlg, 200
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Local bmp$
bmp$ = GetBitString("cowgirl.bmp")
Graphic Set Bits bmp$
End If
End Function
Function GetBitString(fSource As String) As String
Local bmpheader As BITMAPFILEHEADER, bmpinfo As BITMAPINFOHEADER, padding, i as Long
Local w,h AS Long, bmp$
'Open the file
Open fSource For Binary as #1
Get #1, , bmpheader 'get BITMAPFILEHEADER
Get #1, , bmpinfo 'get BITMAPINFOHEADER
'Create single-dimension buffer for bitmap color data (BGR data + padding)
w = bmpinfo.biWidth
h = bmpinfo.biHeight
padding = (4 - (w*3) Mod 4) Mod 4
Dim Buffer((w*3+padding)*h - 1) As Byte 'pixel color array data (0-based array)
'read the buffer - padded, upside-down, BGR image color data
Get #1, bmpheader.bfOffBits+1, Buffer() 'read image data into structure start,length,variable
Close #1
'Example of modifying the image color data within the Buffer() array
For i = 0 to UBound(Buffer) 'loop through all Bytes
Next i
Function = bmp$
End Function
'gbs_00524
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm