Convert your buttons to BMPs keeping transparency with GdiPlusX -REVISITED

this is just an update for an old post with the same title. erik gomez and russel campbell had some problems on the conversions of some specific png images, so i recoded this function, this time using a safer code.

new update:

thanks to bernard bout and craig boyd, i've updated the code below. in fact it contained a small bug, that was not adjusting the white colors. now it seems to be working nice. thanks !

this runs a little bit slower than the other aproach, but in my tests, the success was 100%.

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.

Important Requires VFP9 and GdiplusX to run.  

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 forget to remove the locfile() command, and just use a do system.app instead

lparameters tcsourcefile, tcdestfile

* tcsourcefile = getpict()
* tcdestfile = forceext(tcsourcefile, "bmp")

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
local lodestbmp as xfcbitmap

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

 lodestbmp = .bitmap.new(lobmp.width, lobmp.height, .imaging.pixelformat.format24bpprgb)
 lodestgfx = .graphics.fromimage(lodestbmp)

 * clear the new bitmap

 * by craig boyd - for enhancing the smoothless and quality
 lodestgfx.smoothingmode     = .drawing2d.smoothingmode.highquality
 lodestgfx.interpolationmode = .drawing2d.interpolationmode.highqualitybicubic
 lodestgfx.pixeloffsetmode   = .drawing2d.pixeloffsetmode.highquality

 lorect = lobmp.getbounds()

 * get the top left pixel color, presuming this color is the background color 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)

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

 lodestgfx.drawimage(lodestbmp, lorect, lorect, .graphicsunit.pixel, loattr)

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