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.
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 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
loDestGfx.Clear(.Color.White)
* 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)
loAttr.SetRemapTable(loColorMap)
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
loAttr.SetRemapTable(loColorMap)
loDestGfx.DrawImage(loDestBmp, loRect, loRect, .GraphicsUnit.Pixel, loAttr)
loDestBmp.Save(tcDestFile, .Imaging.ImageFormat.Bmp)
ENDWITH