WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Making Bitmap files via GDI calls


I think this one should work on 32bit mode too, the compression had to be set to BI_RGB, i also removed the 256 color thing on 16bit, it was not necessary.

the only utility i can think for this now is to make graphic reports, bars and things like that, maybe to display in html pages.

gdi32=dllload(strcat(dirwindows(1),"gdi32.dll"))
user32=dllload(strcat(dirwindows(1),"user32.dll"))

DIB_RGB_COLORS=0
BI_RGB=0

szBITMAPINFOHEADER=40
szBITMAPFILEHEADER=14
szRGBQUAD=4
szBITMAPINFO=8

WHITE_BRUSH=0
TRANSPARENT=1

RECT=binaryalloc(16)

bitmap="mybitmap.bmp"
;dimension
bw=150
bh=150

binarypoke4(RECT,8,bw)
binarypoke4(RECT,12,bh)

;white brush
hbwhite=dllcall(gdi32,long:"GetStockObject",long:WHITE_BRUSH)

;font
fheight=15
hfverd=dllcall(gdi32,long:"CreateFontA",long:fheight,long:0,long:0,long:0,long:0,long:0,long:0,long:0,long:0,long:0,long:0,long:0,long:0,lpstr:"Verdana")
;message("",hfverd)

;desktop DC
hwscreen=dllcall(user32,long:"GetDesktopWindow")
hdc=dllcall(user32,long:"GetDC",long:hwscreen)

;compatible memory DC
hmdc=dllcall(gdi32,long:"CreateCompatibleDC",long:hdc)

;font
dllcall(gdi32,long:"SelectObject",long:hmdc,long:hfverd)
;background mode
dllcall(gdi32,long:"SetBkMode",long:hmdc,long:TRANSPARENT)
;text color
dllcall(gdi32,long:"SetTextColor",long:hmdc,long:255) ;red

;compatible bitmap
hbm=dllcall(gdi32,long:"CreateCompatibleBitmap",long:hdc,long:bw,long:bh)
dllcall(gdi32,long:"SelectObject",long:hmdc,long:hbm)

;fill background
dllcall(user32,long:"FillRect",long:hmdc,lpbinary:RECT,long:hbwhite)

;text
text="Hello World !"
dllcall(gdi32,long:"TextOutA",long:hmdc,long:0,long:0,lpstr:text,long:strlen(text))

;write bitmap
ctsize=256 
szinfo=(szBITMAPINFOHEADER)+(ctsize*szRGBQUAD)
info=binaryalloc(szinfo)
binarypoke4(info,0,szBITMAPINFOHEADER)

;fill BITMAPINFO
dllcall(gdi32,long:"GetDIBits",long:hdc,long:hbm,long:0,long:0,lpnull,lpbinary:info,long:DIB_RGB_COLORS)
binaryeodset(info,szinfo)

biBitCount=binarypeek2(info,14)
;message("",biBitCount)

biSizeImage=binarypeek4(info,20)
;message("",biSizeImage)

binarypoke4(info,16,BI_RGB) ;biCompression

select biBitCount
  case 4
    ctsize=16
    break

  case 8
    ctsize=256
    break

  case 16
    ctsize=0
    break

  case 24
    ctsize=0
    break

  case 32
    ctsize=0
    break
endselect

bits=binaryalloc(biSizeImage)

;get bitmap bits
dllcall(gdi32,long:"GetDIBits",long:hdc,long:hbm,long:0,long:bh,lpbinary:bits,lpbinary:info,long:DIB_RGB_COLORS)
binaryeodset(bits,biSizeImage)

finfo=binaryalloc(szBITMAPFILEHEADER)

binarypokestr(finfo,0,"BM") ;bfType
                    
bfOffBits=szBITMAPFILEHEADER+szBITMAPINFOHEADER+(szRGBQUAD*ctsize)
binarypoke4(finfo,10,bfOffBits) ;bfOffBits
binarypoke4(finfo,2,bfOffBits+biSizeImage) ;bfSize
;bfReserved1=0
;bfReserved2=0

BinaryWriteEx(finfo,0,bitmap,0,szBITMAPFILEHEADER)
BinaryWriteEx(info,0,bitmap,szBITMAPFILEHEADER,szBITMAPINFOHEADER+(szRGBQUAD*ctsize))
BinaryWriteEx(bits,0,bitmap,szBITMAPFILEHEADER+szBITMAPINFOHEADER+(szRGBQUAD*ctsize),biSizeImage)

;free
binaryfree(finfo)
binaryfree(info)
binaryfree(bits)
binaryfree(RECT)

dllcall(user32,long:"ReleaseDC",long:hwscreen,long:hdc)
dllcall(gdi32,long:"DeleteObject",long:hbm)
dllcall(gdi32,long:"DeleteObject",long:hfverd)
dllcall(gdi32,long:"DeleteDC",long:hmdc)

dllfree(gdi32)
dllfree(user32)

message("","done")


Article ID:   W15992
File Created: 2004:03:30:15:42:08
Last Updated: 2004:03:30:15:42:08