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

Samples from Users
plus
plus
plus
plus
plus
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.

Write Text on a Bitmap


;-------------------------------------------------------------;
;RGB : Returns a color value                                  ;
;-------------------------------------------------------------;
;red   : intensity of red, from 0 to 255                      ;
;green : intensity of green, from 0 to 255                    ;
;blue  : intensity of blue, from 0 to 255                     ;
;-------------------------------------------------------------;
;Returns : A color value, to use with bmStampText()           ;
;-------------------------------------------------------------;
#definefunction rgb(red,green,blue)
  return ((red mod 256)+(green mod 256)*256+(blue mod 256)*256*256)
#endfunction

;---------------------------------------------------------------------;
;bmStampText : Stamps text over a bitmap file, creating a new bitmap. ;
;---------------------------------------------------------------------;
;x          : text x position in the bitmap                           ;
;y          : text y position in the bitmap                           ;
;color      : text color, returned by RGB()                           ;
;fheight    : font height                                             ;
;fweight    : weight of the font in the range 0 through 1000.         ;
;             For example, 400 is normal and 700 is bold.             ;
;             If this value is zero, a default weight is used.        ;
;fitalic    : Specifies an italic font if set to @TRUE.               ;
;funderline : Specifies an underlined font if set to @TRUE.           ;
;fstrikeout : Specifies a strikeout font if set to @TRUE.             ;
;fface      : Typeface name of the font. The length of this string    ;
;             must not exceed 32 characters.                          ;
;bmsource   : source bitmap                                           ;
;bmtarget   : target bitmap to create                                 ;
;---------------------------------------------------------------------;
;Returns    : bytes written to the target bytmap                      ;
;---------------------------------------------------------------------;
#definefunction bmStampText(x,y,color,text,fheight,fweight,fitalic,funderline,fstrikeout,fface,bmsource,bmtarget)
gdi32=dllload(strcat(dirwindows(1),"gdi32.dll"))
user32=dllload(strcat(dirwindows(1),"user32.dll"))

DIB_RGB_COLORS=0
BI_RGB=0

LR_LOADFROMFILE=16
IMAGE_BITMAP=0
LR_CREATEDIBSECTION=8192

szBITMAPINFOHEADER=40
szBITMAPFILEHEADER=14
szRGBQUAD=4

TRANSPARENT=1

hbm=dllcall(user32,long:"LoadImageA",long:0,lpstr:bmsource,long:IMAGE_BITMAP,long:0,long:0,long:LR_CREATEDIBSECTION|LR_LOADFROMFILE)
if hbm==0 ;error
  dllfree(gdi32)
  dllfree(user32)
  return
endif

;font
hfont=dllcall(gdi32,long:"CreateFontA",long:fheight,long:0,long:0,long:0,long:fweight,long:fitalic,long:funderline,long:fstrikeout,long:0,long:0,long:0,long:0,long:0,lpstr:fface)

;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:hfont)
;background mode
dllcall(gdi32,long:"SetBkMode",long:hmdc,long:TRANSPARENT)
;text color
dllcall(gdi32,long:"SetTextColor",long:hmdc,long:color)

;bitmap
dllcall(gdi32,long:"SelectObject",long:hmdc,long:hbm)

;text
dllcall(gdi32,long:"TextOutA",long:hmdc,long:x,long:y,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)

biSizeImage=binarypeek4(info,20)

binarypoke4(info,16,BI_RGB) ;biCompression

biHeight=binarypeek4(info,8)

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:biHeight,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

a=BinaryWriteEx(finfo,0,bmtarget,0,szBITMAPFILEHEADER)
b=BinaryWriteEx(info,0,bmtarget,szBITMAPFILEHEADER,szBITMAPINFOHEADER+(szRGBQUAD*ctsize))
c=BinaryWriteEx(bits,0,bmtarget,szBITMAPFILEHEADER+szBITMAPINFOHEADER+(szRGBQUAD*ctsize),biSizeImage)

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

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

dllfree(gdi32)
dllfree(user32)

return a+b+c
#endfunction


;------------------------------------------
;TEST
;------------------------------------------

source="C:\Temp\bitmap.bmp"
target="C:\Temp\bitmapout.bmp"
color=rgb(255,255,255) 
fheight=28
fweight=700
fitalic=0
funderline=0
fstrikeout=0
fface="Tahoma"
text="Hi Cam-Era !"
;
r=bmStampText(10,10,color,text,fheight,fweight,fitalic,funderline,fstrikeout,fface,source,target)
;
message(r,"done")

Article ID:   W16711
File Created: 2005:02:18:12:21:56
Last Updated: 2005:02:18:12:21:56