Foxite.COM Community Weblog

Foxite.COM Community Weblog - free weblog service for the Visual FoxPro Community.
Welcome to Foxite.COM Community Weblog Sign in | Join | Help
in
Home Blogs Forum Photos Forum Archives

VFP IMAGING



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

 

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
Published Wednesday, November 14, 2007 2:23 PM by cesarchalom
Filed Under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Carlos Alloatti said:

Very nice, thank you.
November 14, 2007 8:05 PM
 

Bernard Bout said:

Very useful for me. Will speed up my images creation, plus no more masks. Thanks Cesar.
November 15, 2007 3:39 PM
 

Carlos Alloatti said:

In this article, we will try to find out which is the best image format to use in VFP controls, and explain some concepts about images in general, from  a VFP developer point of view.
November 16, 2007 4:22 AM
 

Carlos Alloatti said:

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 !

November 16, 2007 4:07 PM
 

Nestor Buelva Jr. said:

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.

Notes:
- The PNG/ICO format is RGB/+A (32 bpp)
February 22, 2008 3:11 AM
 

Nestor Buelva Jr. said:

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?

Thanks
March 24, 2008 4:05 AM
 

Jim Vahl said:

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

April 12, 2008 8:45 PM

What do you think?

(required) 
(optional)
(required)