Page 1 of 1

Adding Text to BMP File??

Posted: Mon Jul 13, 2026 3:04 pm
by bradmaudlin

Hello,
I am trying to put a text string on a bitmap picture and not getting it to work.
I am using FW 25.12 + Harbour + BCC7.70.

This is what I am trying to do:

  1. Open existing BMP file.
  2. Add a transparent Text string to the BMP.
  3. Save new BMP file (with Text on it).

The picture does not even need to be displayed.

This is a sample from ChatGPT.
I pass in a BMP File Name and a Text String.

// TBitmap Version
Function AddReceipt2BMP(m_BMP,m_Receipt)
Local oBmp
Local oDC
Local hOldBmp
Local hFont
Local hOldFont


//Load BMP
oBmp := TBitmap():Define(m_BMP)


IF oBMP == NIL
// Can't open BMP File, Get Out!
Return
EndIf


// Memory DC (No idea what this does)
oDC := CreateCompatibleDC( 0 )


hOldBmp := SelectObject( oDC:hDC, oBmp:hBitmap ) // Errors on this Line!!!!!!


// Set drawing attributes
SetBkMode( oDC:hDC, 1 ) // Transparent
SetTextColor( oDC:hDC, CLR_YELLOW )


// Create Font
hFont := CreateFont(-28,0,0,0,700,0,0,0,ANSI_CHARSET,0,0,0,0,"Courier New")


hOldFont := SelectObject(oDC:hDC, hFont)


//Draw Text
TextOut(oDC:hDC, hOldFont,40,40,m_Receipt,len(m_Receipt))

// Cleanup font
SelectObject( oDC:hDC, hOldFont )
DeleteObject( hFont )

// Restore bitmap
SelectObject( oDC:hDC, hOldBmp )

// Save bitmap
oBmp:SaveToFile( "output.bmp" )

DeleteDC( oDC:hDC )
oBmp:End()


Return

I get an Error on -> hOldBmp := SelectObject( oDC:hDC, oBmp:hBitmap )
Error description: Error BASE/1004 No exported method: HDC

Does anyone have a sample function to add text to a BMP??

Thanks,
Brad Maudlin


Re: Adding Text to BMP File??

Posted: Mon Jul 13, 2026 7:58 pm
by Cgallegoa

Brad,
Look at the TextOnImage() function in this working example:

Code: Select all | Expand

#Include "FiveWin.ch"

FUNCTION Main()
   LOCAL aGrad, oImg
   LOCAL Siz := { 600, 800 }, oFont1, oDlg
   SET DATE FORMAT "yyyy-mm-dd"
   aGrad := { { 0.30, CLR_WHITE, CLR_WHITE },{ 0.50, CLR_WHITE, CLR_WHITE } }
   DEFINE FONT oFont1 NAME "Verdana" SIZE 0,-20 BOLD
   DEFINE DIALOG oDlg FROM 0, 0 TO Siz[1], Siz[2] PIXEL TRUEPIXEL ;
      STYLE nOR( WS_POPUP, WS_BORDER ) GRADIENT aGrad
      @ 0,0 XIMAGE oImg FILE "D:\FWH\BITMAPS\PNGS\2.png" SIZE 0,0 OF oDlg NOBORDER
      oImg:bPainted:= { |hDC| TextOnImage(hdc,oFont1,"Text over img") }
      oImg:SetColor( CLR_GRAY, CLR_HRED ) // Added Mister Nages
   ACTIVATE DIALOG oDlg CENTERED
   RELEASE FONT oFont1
RETURN NIL
//-----------------------------------------------------------------//

Function  TextOnImage(hdc,oFont1,cCod)
           FW_SayText( hDC, cCod, {350,320,380,490 },,oFont1, CLR_GREEN,  , .f. )  // x1,y1 hasta x2,y2
           FW_SayText( hDC, DTOC(Date()), {380,340,410,470 },,oFont1, CLR_HBLUE,  , .f. )  // x1,y1 hasta x2,y2
return nil
//-----------------------------------------------------------------//

Regards,


Re: Adding Text to BMP File??

Posted: Tue Jul 14, 2026 4:11 pm
by bradmaudlin

Thank you!
This sample does what I need and I should be able to use it in my software.
It looks so simple now!

Again, thanks.
Brad


Re: Adding Text to BMP File??

Posted: Tue Jul 14, 2026 6:56 pm
by karinha

// C:\FWH2603\SAMPLES\OLGA1.PRG

Code: Select all | Expand

#include "FiveWin.ch"

FUNCTION TextOnImage()

   LOCAL hBmp, oFont, oLarg, oSmal
   LOCAL cSrcImage := "c:\fwh2603\bitmaps\olga1.jpg"
   LOCAL cDstImage := "Ambienty.jpg"
   LOCAL cText     := "Ambienty" + CRLF + "Paisagismo"

   IF FILE( "Ambienty.jpg" )
      DELETEFILE( "Ambienty.jpg" )
   ENDIF

   DEFINE FONT oLarg NAME "VERDANA" SIZE 0, - 50 BOLD
   DEFINE FONT oFont NAME "TAHOMA"  SIZE 0, - 30 BOLD
   DEFINE FONT oSmal NAME "TAHOMA"  SIZE 0, - 16 BOLD

   hBmp  :=  FW_MakeYourBitmap( 352, 450, < |hDC, w, h |

   FW_DrawImage( hDC, cSrcImage, { 0, 0, h, w } )
   FW_SayText( hDC, cText, { 0, 0, h - 30, w }, "B", { oLarg, oFont } )
   FW_SayText( hDC, "FWH Image Functions", { 30, 0, h, w }, "T", oSmal )
   RETURN nil
)

   FW_SaveImage( hBmp, cDstImage, 100 )

   XImage( cDstImage ) // Image modified

   // cleanup
   DeleteObject( hBmp )
   RELEASE FONT oLarg, oFont, oSmal

RETURN NIL

// FIN / END

Regards, saludos.