Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Convert PNG to BMP and MSK files
Message
 
To
05/07/2005 10:10:49
General information
Forum:
Visual FoxPro
Category:
Pictures and Image processing
Miscellaneous
Thread ID:
01028982
Message ID:
01029141
Views:
16
This message has been marked as the solution to the initial question of the thread.
Malcolm,

Here's how you can do it with GDI+. Set the first 3 variables or change them into parameters.

This will convert the PNG into a BMP and create a MSK file where all the non-tranparent pixels in the PNG are converted to black.

Hope this helps,
Bo Durban
www.moxiedata.com
#define PixelFormat24bppRGB  0x00021808
#define UnitPixel    2
#define ARGB_WHITE   0xffffffff

cImagePng = "image.png"
cImageBmp = FORCEEXT(cImagePng,"bmp")
cImageMsk = FORCEEXT(cImageBmp,"msk")

SET CLASSLIB TO (HOME()+"ffc\_gdiplus") ADDITIVE

** Load our PNG and save it as a BMP
oImg = CREATEOBJECT("GpImage")
eStat = oImg.CreateFromFile(cImagePng)
eStat = oImg.SaveToFile(cImageBmp,"image/bmp")

nWidth = oImg.ImageWidth
nHeight = oImg.ImageHeight

** Create a bitmap the same size
oBmp = CREATEOBJECT("GpBitmap")
eStat = oBmp.Create(nWidth, nHeight, PixelFormat24bppRGB)
** Load it into a graphics object
oGfx = CREATEOBJECT("GpGraphics")
eStat = oGfx.CreateFromImage(oBmp)
oGfx.Clear(ARGB_WHITE)

** Create an GpImageAttribute to translate the colors
hImgAttrib = 0
GdipCreateImageAttributes(@hImgAttrib)

** Convert all RGB values to black
cMatrix = ;
   BINTOC(1,"F")+BINTOC(0,"F")+BINTOC(0,"F")+BINTOC(0,"F")+BINTOC(0,"F")+;
   BINTOC(0,"F")+BINTOC(1,"F")+BINTOC(0,"F")+BINTOC(0,"F")+BINTOC(0,"F")+;
   BINTOC(0,"F")+BINTOC(0,"F")+BINTOC(1,"F")+BINTOC(0,"F")+BINTOC(0,"F")+;
   BINTOC(0,"F")+BINTOC(0,"F")+BINTOC(0,"F")+BINTOC(1,"F")+BINTOC(0,"F")+;
   BINTOC(-1,"F")+BINTOC(-1,"F")+BINTOC(-1,"F")+BINTOC(0,"F")+BINTOC(1,"F")
GdipSetImageAttributesColorMatrix(hImgAttrib,1,1,cMatrix,0,0)

** Draw the PNG to our mask, converting all RGB values to black
GdipDrawImageRectRect(oGfx.GetHandle(),oImg.GetHandle(),;
   0, 0, nWidth, nHeight, ;
   0, 0, nWidth, nHeight, ;
   UnitPixel, hImgAttrib, 0, 0)

** Save the MSK file
oBmp.SaveToFile(cImageMsk,"image/bmp")

GdipDisposeImageAttributes(hImgAttrib)
oGfx = NULL
oBmp = NULL
oImg = NULL



***********************************************
FUNCTION DeclGDIP()

   DECLARE Long GdipDrawImageRectRect IN GDIPLUS ;
      Long graphics, Long image, ;
      Single dstx, Single dsty, ;
      Single dstwidth, Single dstheight, ;
      Single srcx, Single srcy, ;
      Single srcwidth, Single srcheight, ;
      Long srcUnit, Long imageAttributes, ;
      Long callback, Long callbackData

   DECLARE Long GdipCreateImageAttributes IN GDIPLUS ;
      Long @imageattr

   DECLARE Long GdipDisposeImageAttributes IN GDIPLUS ;
      Long imageattr

   DECLARE Long GdipSetImageAttributesColorMatrix IN GDIPLUS ;
      Long imageattr, Long type, ;
      Long enableFlag, String colorMatrix, ;
      Long grayMatrix, ;
      Long flags

ENDFUNC
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform