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, 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.
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.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
Very nice, thank you.
ReplyDeleteVery useful for me. Will speed up my images creation, plus no more masks. Thanks Cesar.
ReplyDeleteCesar: what does "loCloned = loBmp.Clone()" do?
ReplyDeleteHi 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 !
Great tools, it is very valuable to me. Thanks
ReplyDeleteBut ..... 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.
Notes:
- The PNG/ICO format is RGB/+A (32 bpp)
Great articles. I love it very much.
ReplyDeleteI'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?
Thanks
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.
ReplyDeleteI 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
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?
ReplyDeleteHi Russel,
Sorry for taking so much time to answer.
Can you provide some more information about this problem ? Like the line number? TIA
Cesar
It occurs in this function:
ReplyDeleteFUNCTION ColorMap_ACCESS
IF VARTYPE(This.ColorMap) <> "O"
This.ColorMap = CREATEOBJECT("xfcColorMap")
ENDIF
RETURN THIS.ColorMap
ENDFUNC
The line of code from your conversion program is this one:
loColorMap = .Imaging.ColorMap.New()
Hope this helps.
Cesar - I posted the info you wanted. Any update on this?
ReplyDeleteHi Erik,
Thanks for reporting your difficulties.
I rebuilt this function, and you'll find the new source here:
http://weblogs.foxite.com/vfpimaging/archive/2008/12/15/7441.aspx
Hope this helps!
Cesar
Hi,
ReplyDeleteI’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:
http://weblogs.foxite.com/vfpimaging/archive/2008/12/15/7441.aspx
Hope this helps!
Cesar
thanks,
Erik Gomez