2007-11-24

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

Important Requires vfp9 and GdiplusX to run. 


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



4 comments:

  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.

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


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

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


    ReplyDelete
  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?



    ReplyDelete
  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.

    ReplyDelete