How to create text as image file with GdiPlusX

updated: fixed image size, thanks to christof wollenhaupt


more than once i've seen people asking to create images containing some text. the sample below is really very simple.

  • creates a font
  • measures the space that the text will need
  • creates an image with the needed size
  • draws the string
  • saves to disk

requires vfp9 and gdiplusx to run. 
please make sure that you have the latest version, because this sample may be using some functions that were added or fixed recently.


do locfile("system.prg")

with _screen.system.drawing
   local lctext
   lctext = "gdiplusx is cool !!!"

   * create a font
   local lofont as xfcfont
   lofont =
_screen.system.drawing.font.new("verdana", 32, .fontstyle.bolditalic)

   local lotmpbmp as xfcbitmap
   lotmpbmp = .

   * retrieve the graphics object.
   local lotmpgfx as xfcgraphics
   lotmpgfx = .graphics.fromimage(lotmpbmp)

   * measure the string
   * get the size required for our text
   local losize as xfcsize
   losize = lotmpgfx.measurestring(lctext, lofont)

   local lonewbmp as xfcbitmap
   lonewbmp = .

   local lonewgfx as xfcgraphics
   lonewgfx = .graphics.fromimage(lonewbmp)

   * clear the background to yellow

   * create a solid brush
   local lobrush as xfcsolidbrush
   lobrush = .solidbrush.new(.
color.fromrgb(255,0,0)) && red

   * create an stringformat object in order to draw the sting centered in the image
   local lostringfmt as xfcstringformat
   lostringfmt = .stringformat.new()
alignment = .stringalignment.center

   * create a rectangle with the measures of the bitmap
   local lorect as xfcrectanglef
   lorect = lonewbmp.getbounds()

   * draw the string
   lonewgfx.drawstring(lctext, lofont, lobrush, lorect, lostringfmt)

   * finally save the image
   lonewbmp.save("c:\mytext.png", .imaging.imageformat.png)

   * show the image
   run /n explorer.exe c:\mytext.png


  1. Can you expand this to actually draw this onto a surface like a form or container etc?

    What I mean is the sample saves the text, but I want to be able to draw it directly onto a surface, without saving it as a PNG etc.

  2. Este articulo esta traducido al español en www.PortalFox.com en:

    --- Como crear texto como un archivo de imagen con GdiPlusX ---


  3. Can't get it to work, the following line is causing an error:

    loNewBmp = .Bitmap.New(loSize.Ceiling)

    The error is "property not found". As far as I have searched through the code, ceiling is a method and should receive a parameter? But it it is so, what is needed to pass to ceiling?

  4. Hi Cesar... how can i do this... but with a halo fx and alpha fx??...thanks for sharing yours tips... hace un calor en Paraguay!!!.saludos... greetings
    Of course you can !
    Have a look at the GdiPlusX samples, there we put 5 or 6 forms with many kinds of texts and characters.