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
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
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)
loAttr.SetRemapTable(loColorMap)
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
loAttr.SetRemapTable(loColorMap)
loGfx.DrawImage(loBmp, m.loRect, m.loRect, .GraphicsUnit.Pixel, loAttr)
loBmp.Save(tcDestFile, .Imaging.ImageFormat.Bmp)
ENDWITH