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

important
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.
http://www.codeplex.com/vfpx/wiki/view.aspx?title=gdiplusx&referringtitle=home

 

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 = .
bitmap.new(1,1)

   * 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 = .
bitmap.new(losize.ceiling)

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

   * clear the background to yellow
   lonewgfx.clear(.color.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()
   lostringfmt.
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
endwith
 

5 Responses to How to create text as image file with GdiPlusX

  • Bernard Bout says:

    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.

  • Luis Maria says:

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

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

    http://www.portalfox.com/article.php?sid=2538

  • Thomas Holste says:

    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?

  • Guillermo says:

    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.

  • farhad shahbaz says:

    Hi dear manager
    pls tel me in this line:
    do locfile(“system.prg”)
    where is “”system.prg””

    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *