Convert your buttons to BMPs keeping transparency with GdiPlusX

updated code - fixed bug reported by jean pierre senet in the graphics initialization. merci beaucup jean !


the function below converts any button image to a bmp to be used in vfp forms.

there are lots of cool and free icons available on the web, but the vast majority are in .ico, gif or png image formats, that are not very familiar and reliable to be used in vfp. for us, the best image format, for a lot of reasons, is the bmp format.

some transformations are needed to make this bmp to show exactly how we desire, specially when converting source images in a png, gif or ico formats.

vfp shows the pure white - rgb(255,255,255) as transparent in our buttons and image objects. the code below first converts the original whites to rgb(254,254,254) that is visually the same, but does not become transparent, and eliminates the need to create a mask image (.msk) and next, converts the background color of the original bitmap to pure white, that will show transparent in vfp forms.

for more details, please check these prior posts:

bmps with transparent backgrounds

how to put one image over another in a form


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.


save the program below as button2bmp.prg, and call it this way:

button2bmp(getpict(), "c:\newicon.bmp")

when you compile this program in your executable, please don't dorget to remove the locfile() command, and just use a do system.prg instead


lparameters tcsourcefile, tcdestfile

do locfile("system.app")

local lobmp as xfcbitmap
local logfx as xfcgraphics
local loborderclr as xfccolor
local lorect as xfcrectangle
local loattr as xfcimageattributes
local locolormap as xfccolormap

with _screen.system.drawing
locolormap = .imaging.colormap.new()
loattr = .imaging.imageattributes.new()
   lobmp = .bitmap.fromfile(tcsourcefile)
   logfx = .graphics.fromimage(lobmp)
lorect = lobmp.getbounds()

* get the top left pixel color, presuming this color is the background colro to become transparent
* for our bmp case, this will become pure white - rgb(255,255,255)
* that becomes transparent when used in vfp objects
loborderclr = lobmp.getpixel(0,0)

* convert original whites rgb(255,255,255) to off white - rgb(254,254,254)
* this way, the whites will remain without the need of a mask
locolormap.oldcolor = .color.white
locolormap.newcolor = .color.fromargb(255,254,254,254)

logfx.drawimage(lobmp, lorect, lorect, .graphicsunit.pixel, loattr)

* next step, convert the borders to pure white, rgb(255,255,255) that will become transparent in buttons
locolormap.oldcolor = loborderclr
locolormap.newcolor = .color.white
logfx.drawimage(lobmp, m.lorect, m.lorect, .graphicsunit.pixel, loattr)

lobmp.save(tcdestfile, .imaging.imageformat.bmp)


  1. Very useful for me. Will speed up my images creation, plus no more masks. Thanks Cesar.

  2. Cesar: what does "loCloned = loBmp.Clone()" do?
    Hi Carlos,
    This sample is just an adapted code from my last blog post.... And I missed to delete that line ! I've just updated the code.
    Thanks for catching that !

  3. Great tools, it is very valuable to me. Thanks

    But ..... not all the GIF, PNG, and ICO can be process corectly with this code.

    Sometimes instead of white, the backround color becomes black.

    How can I deal with that.


    - The PNG/ICO format is RGB/+A (32 bpp)

  4. Great articles. I love it very much.

    I'm not very familiar with GDI+ but I'm still learning to using it.

    When I try this code to convert FamFam Silk Icons (32bpp PNG format), the result was not WHITE background but turn to be BLACK background. What modifications need todo with this sample code to convert the 32 bpp PNG icon to BMP with transparency?


  5. This is a great idea!  We would love to reduce the number of image files in our app.  But I can't get the above code to run without error.  No matter what the size or format of the source file, the code fails with an OutOfMemory error in the xfcGdipGetImageGraphicsContext method.

    I am running on XP.  I'm using the latest version of GDPlusx. The version of gdiplus.dll is 5.1.3102.2180.  Could there be a bug in the DLL which has been corrected in a later version?  Is there a more recent version available?

    By the way, we are very happily using gradient objects on our forms withoiut any difficulties.  Thank you!
    Thanks very much for your kind comments.
    Is your source image a GIF ?
    Can you send me one of those ? Please send to vfpimaging at hotmail dot com

  6. This code fails with "Cannot find xfcobject in system.vcx."  I have already installed the latest version of GDIPlusX (today).  Can you tell me what's going on?
    Hi Russel,
    Sorry for taking so much time to answer.
    Can you provide some more information about this problem ? Like the line number? TIA

  7. It occurs in this function:


      IF VARTYPE(This.ColorMap) <> "O"

         This.ColorMap = CREATEOBJECT("xfcColorMap")


      RETURN THIS.ColorMap


    The line of code from your conversion program is this one:

    loColorMap = .Imaging.ColorMap.New()

    Hope this helps.

  8. Cesar - I posted the info you wanted.  Any update on this?
    Hi Erik,
    Thanks for reporting your difficulties.
    I rebuilt this function, and you'll find the new source here:
    Hope this helps!

  9. Hi,

    I’ve been trying to use the code in your blog: Convert your buttons to BMPs keeping transparency with GdiPlusX to convert PNG files to BMPs with transparency, but to no avail.
    Hi Erik,
    Thanks for reporting your difficulties.
    I rebuilt this function, and you'll find the new source here:
    Hope this helps!

    Erik Gomez