2007-07-27

Draw Logos in your images with GdiPlusX - Part 2

Here are 5 new samples derived from the ones that I showed in a previous post.



For all the samples that I'll provide the vfpx logo will be drawn in some bigger pictures. to show all the flexibility that Gdi+ can offer to us, some effects will be aplied to the logo.








Important


Requires VFP9 and GdiplusX to run. 
Please make sure that you have the latest version!

https://github.com/VFPX/GDIPlusX


sample 5:

aply 100% transparency to the white color, to eliminate the background
draw the logo aplying predefined colormatrix that will the following transformation: convert to greyscale 50% transparency to the whole image
position: top left



do locfile("system.app") with _screen.system.drawing as xfcdrawing

local lcmainpict, lclogopictlocal lomainbmp as xfcbitmaplocal lologobmp as xfcbitmaplocal logfx as xfcgraphics

lcmainpict = getpict()
lclogopict =
getpict()
lomainbmp = .
bitmap.fromfile(lcmainpict)
lologobmp = .
bitmap.fromfile(lclogopict)
logfx = .graphics.fromimage(lomainbmp)
 
*!* sample 5
*!* aply 100% transparency to the white color, to eliminate the background
*!* draw the logo aplying predefined colormatrix that will
*!* the following transformation: convert to greyscale 50% transparency to the whole image
*!* position: top left


* first step: eliminate the white backgroundlologobmp.maketransparent(.color.white)

* define the transparency ratio that will be aplied
* this parameter ranges from 0 (totally transparent) to 1 (totally opaque)
local lntranspratio
lntranspratio = 0.50
&& 50%

* create a colormatrix that will have the transformations information
* the position (4,4) of the matrix is responsible for the opacity
local loclrmatrix as xfccolormatrix
loclrmatrix = .imaging.colormatrix.new( ;
   0.33, 0.33, 0.33, 0 , 0, ;
   0.33, 0.33, 0.33, 0 , 0, ;
   0.33, 0.33, 0.33, 0 , 0, ;
   0, 0, 0, lntranspratio, 0, ;
   0, 0, 0, 0 , 0)


* create an image attributes object to create the effects based in our clrmatrixlocal loattr as xfcimageattributes
loattr = .imaging.imageattributes.new()
loattr.setcolormatrix(loclrmatrix)


* we need to create a rectangle that will contain the coordinates and size of the transformed logolocal lorect as xfcrectangle
lorect = .rectangle.new()
lorect.x = 0
lorect.y = 0
lorect.
width = lologobmp.widthlorect.height = lologobmp.height * draw the transformed image using the rectangle and imgattributes/clrmatrixlogfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.pixel, loattr)
lomainbmp.
save("c:\logo5.jpg", .imaging.imageformat.jpeg)run /n explorer.exe c:\logo5.jpgendwith
return
 


sample 6:

aply 100% transparency to the white color, to eliminate the background
draw the logo aplying predefined colormatrix that will convert to greyscale and 50% transparency to the whole image
position: center
size: expanded 4 times



do locfile("system.app")

with _screen.system.drawing as xfcdrawing

local lcmainpict, lclogopictlocal lomainbmp as xfcbitmaplocal lologobmp as xfcbitmaplocal logfx as xfcgraphics

lcmainpict = getpict()
lclogopict =
getpict()
lomainbmp = .
bitmap.fromfile(lcmainpict)
lologobmp = .
bitmap.fromfile(lclogopict)
logfx = .graphics.fromimage(lomainbmp)
 
*!* sample 6
*!* aply 100% transparency to the white color, to eliminate the background
*!* draw the logo aplying predefined colormatrix that will
*!* the following transformation: convert to greyscale 50% transparency to the whole image
*!* position: center
*!* size: expanded 4 times


* first step: eliminate the white backgroundlologobmp.maketransparent(.color.white)

* define the transparency ratio that will be aplied
* this parameter ranges from 0 (totally transparent) to 1 (totally opaque)
local lntranspratio
lntranspratio = 0.25
&& 25%

* create a colormatrix that will have the transformations information
* the position (4,4) of the matrix is responsible for the opacity
local loclrmatrix as xfccolormatrix
loclrmatrix = .imaging.colormatrix.new( ;
   0.33, 0.33, 0.33, 0 , 0, ;
   0.33, 0.33, 0.33, 0 , 0, ;
   0.33, 0.33, 0.33, 0 , 0, ;
   0, 0, 0, lntranspratio, 0, ;
   0, 0, 0, 0 , 0)


* create an image attributes object to create the effects based in our clrmatrixlocal loattr as xfcimageattributes
loattr = .imaging.imageattributes.new()
loattr.setcolormatrix(loclrmatrix)


* we need to create a rectangle that will contain the coordinates and size of the transformed logolocal lorect as xfcrectangle
lorect = .rectangle.new()
lorect.x = (lomainbmp.
width - lologobmp.width*4) / 2
lorect.y = (lomainbmp.
height - lologobmp.height*4) / 2
lorect.
width = lologobmp.width * 4
lorect.
height = lologobmp.height * 4* draw the transformed image using the rectangle and imgattributes/clrmatrixlogfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.pixel, loattr)
lomainbmp.
save("c:\logo6.jpg", .imaging.imageformat.jpeg)

run /n explorer.exe c:\logo6.jpgendwith
return
 


sample 7:

aply 100% transparency to the white color, to eliminate the background
draw the logo aplying 25% opacity to the whole image
position: center
size: expanded 4 times



do locfile("system.app")

with _screen.system.drawing as xfcdrawing

local lcmainpict, lclogopictlocal lomainbmp as xfcbitmaplocal lologobmp as xfcbitmaplocal logfx as xfcgraphics

lcmainpict = getpict()
lclogopict =
getpict()
lomainbmp = .
bitmap.fromfile(lcmainpict)
lologobmp = .
bitmap.fromfile(lclogopict)
logfx = .graphics.fromimage(lomainbmp)
 
*!* sample 7
*!* aply 100% transparency to the white color, to eliminate the background
*!* draw the logo aplying 25% opacity to the whole image
*!* position: center
*!* size: expanded 4 times


* first step: eliminate the white backgroundlologobmp.maketransparent(.color.white)

* define the transparency ratio that will be aplied
* this parameter ranges from 0 (totally transparent) to 1 (totally opaque)
local lntranspratio
lntranspratio = 0.25
&& 25%

* create a colormatrix that will have the transformations information
* the position (4,4) of the matrix is responsible for the opacity
local loclrmatrix as xfccolormatrix
loclrmatrix = .imaging.colormatrix.new( ;
   1, 0, 0, 0 , 0, ;
   0, 1, 0, 0 , 0, ;
   0, 0, 1, 0 , 0, ;
   0, 0, 0, lntranspratio, 0, ;
   0, 0, 0, 0 , 0)


* create an image attributes object to create the effects based in our clrmatrixlocal loattr as xfcimageattributes
loattr = .imaging.imageattributes.new()
loattr.setcolormatrix(loclrmatrix)


* we need to create a rectangle that will contain the coordinates and size of the transformed logolocal lorect as xfcrectangle
lorect = .rectangle.new()
lorect.x = (lomainbmp.
width - lologobmp.width*4) / 2
lorect.y = (lomainbmp.
height - lologobmp.height*4) / 2
lorect.
width = lologobmp.width * 4
lorect.
height = lologobmp.height * 4* draw the transformed image using the rectangle and imgattributes/clrmatrixlogfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.pixel, loattr)
lomainbmp.
save("c:\logo7.jpg", .imaging.imageformat.jpeg)run /n explorer.exe c:\logo7.jpgendwith
return

 


sample 8:

using the techniques shown before, draw logos and text aplying variable transparencies

the code below is not optimized, and is intended only to show some possibilities





do locfile("system.app")

with _screen.system.drawing as xfcdrawing

local lcmainpict, lclogopictlocal lomainbmp as xfcbitmaplocal lologobmp as xfcbitmaplocal logfx as xfcgraphics
lcmainpict =
getpict()
lclogopict =
getpict()
lomainbmp = .
bitmap.fromfile(lcmainpict)
lologobmp = .
bitmap.fromfile(lclogopict)
logfx = .graphics.fromimage(lomainbmp)
 
*!* sample 8
*!* draw image and text in different transparencies
local lcstringlocal lofont as xfcfont
lofont = .
font.new("verdana", 22, .fontstyle.bolditalic)local locolor as xfccolor
locolor = .
color.whitelocal lnxstring
lnxstring = 0 + lologobmp.
width
 
* first step: eliminate the white backgroundlologobmp.maketransparent(.color.white)

* define the transparency ratio that will be aplied
* this parameter ranges from 0 (totally transparent) to 1 (totally opaque)
local lntranspratio

* create a colormatrix that will have the transformations information
* the position (4,4) of the matrix is responsible for the opacity
local loclrmatrix as xfccolormatrix
loclrmatrix = .imaging.colormatrix.new()


* create an image attributes object to create the effects based in our clrmatrixlocal loattr as xfcimageattributes
loattr = .imaging.imageattributes.new()


* we need to create a rectangle that will contain the coordinates and size of the transformed logolocal lorect as xfcrectangle
lorect = .rectangle.new()
lorect.
width = lologobmp.widthlorect.height = lologobmp.height
local
lobrush as xfcsolidbrush
lobrush = .solidbrush.new(locolor)
 
* step 1
* draw image and text 100% opaque
lnopaqueratio = 1 && 100%loclrmatrix.matrix33 = lnopaqueratio
loattr.setcolormatrix(loclrmatrix)
lorect.x = 0
lorect.y = ( lomainbmp.
height / 5 * 1) - lologobmp.heightlogfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.pixel, loattr)
logfx.drawstring("gdiplusx powered - 100% opaque", lofont, lobrush, lnxstring, lorect.y)
 
* step 2
* draw image and text 80% opaque
lnopaqueratio = .80
loclrmatrix.matrix33 = lnopaqueratio
loattr.setcolormatrix(loclrmatrix)
lorect.x = 0
lorect.y = ( lomainbmp.
height / 5 * 2) - lologobmp.heightlocolor.a = lnopaqueratio * 255
lobrush.
color = locolor

logfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.
pixel, loattr)
logfx.drawstring("gdiplusx powered - 80% opaque", lofont, lobrush, lnxstring, lorect.y)
 
* step 3
* draw image and text 60% opaque
lnopaqueratio = .60
loclrmatrix.matrix33 = lnopaqueratio
loattr.setcolormatrix(loclrmatrix)
lorect.x = 0
lorect.y = ( lomainbmp.
height / 5 * 3) - lologobmp.heightlocolor.a = lnopaqueratio * 255
lobrush.
color = locolor

logfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.
pixel, loattr)
logfx.drawstring("gdiplusx powered - 60% opaque", lofont, lobrush, lnxstring, lorect.y)

 
* step 4
* draw image and text 40% opaque
lnopaqueratio = .40
loclrmatrix.matrix33 = lnopaqueratio
loattr.setcolormatrix(loclrmatrix)
lorect.x = 0
lorect.y = ( lomainbmp.
height / 5 * 4) - lologobmp.heightlocolor.a = lnopaqueratio * 255
lobrush.
color = locolor

logfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.
pixel, loattr)
logfx.drawstring("gdiplusx powered - 40% opaque", lofont, lobrush, lnxstring, lorect.y)

 
* step 4
* draw image and text 20% opaque
lnopaqueratio = .20
loclrmatrix.matrix33 = lnopaqueratio
loattr.setcolormatrix(loclrmatrix)
lorect.x = 0
lorect.y = ( lomainbmp.
height / 5 * 5) - lologobmp.heightlocolor.a = lnopaqueratio * 255
lobrush.
color = locolor

logfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.
pixel, loattr)
logfx.drawstring("gdiplusx powered - 20% opaque", lofont, lobrush, lnxstring, lorect.y)


lomainbmp.
save("c:\logo8.jpg", .imaging.imageformat.jpeg)run /n explorer.exe c:\logo8.jpgendwith
return
 




sample 9:

same as previous sample, using the techniques shown before, draw logos and text aplying variable transparencies, using a monochrome logo.

the code below is not optimized, and is intended only to show some possibilities






do locfile("system.app")

with _screen.system.drawing as xfcdrawing

local lcmainpict, lclogopictlocal lomainbmp as xfcbitmaplocal lologobmp as xfcbitmaplocal logfx as xfcgraphics

lcmainpict = getpict()
lclogopict =
getpict()
lomainbmp = .
bitmap.fromfile(lcmainpict)
lologobmp = .
bitmap.fromfile(lclogopict)
logfx = .graphics.fromimage(lomainbmp)
 
*!* sample 9
*!* draw image and text in different transparencies
local lcstringlocal lofont as xfcfont
lofont = .
font.new("verdana", 30, .fontstyle.bolditalic)local locolor as xfccolor
locolor = .
color.whitelocal lobrush as xfcsolidbrush
lobrush = .solidbrush.new(locolor)
 
local lnxstring
lnxstring = 0 + 10 + lologobmp.
width
 
* first step: eliminate the white backgroundlologobmp.maketransparent(.color.white)

* define the transparency ratio that will be aplied
* this parameter ranges from 0 (totally transparent) to 1 (totally opaque)
local lntranspratio* create a colormatrix that will have the transformations information
* the position (4,4) of the matrix is responsible for the opacity


local loclrmatrix as xfccolormatrix
loclrmatrix = .imaging.colormatrix.new( ;
   0.33, 0.33, 0.33, 0 , 0, ;
   0.33, 0.33, 0.33, 0 , 0, ;
   0.33, 0.33, 0.33, 0 , 0, ;
   0, 0, 0, 1 , 0, ;
   0, 0, 0, 0 , 0)
 
* create an image attributes object to create the effects based in our clrmatrixlocal loattr as xfcimageattributes
loattr = .imaging.imageattributes.new()
* we need to create a rectangle that will contain the coordinates and size of the transformed logolocal lorect as xfcrectangle
lorect = .rectangle.new()
lorect.
width = lologobmp.widthlorect.height = lologobmp.height



 
* step 1
* draw image and text 100% opaque
lnopaqueratio = 1 && 100%loclrmatrix.matrix33 = lnopaqueratio
loattr.setcolormatrix(loclrmatrix)
locolor.a = lnopaqueratio * 255
lobrush.
color = locolor
lorect.x = 0
lorect.y = ( lomainbmp.
height / 5 * 1) - lologobmp.heightlogfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.pixel, loattr)
logfx.drawstring("gdiplusx powered", lofont, lobrush, lnxstring, lorect.y)

 
* step 2
* draw image and text 80% opaque
lnopaqueratio = .80
loclrmatrix.matrix33 = lnopaqueratio
loattr.setcolormatrix(loclrmatrix)
lorect.x = 0
lorect.y = ( lomainbmp.
height / 5 * 2) - lologobmp.heightlocolor.a = lnopaqueratio * 255
lobrush.
color = locolor

logfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.
pixel, loattr)
logfx.drawstring("gdiplusx powered", lofont, lobrush, lnxstring, lorect.y)

 
* step 3
* draw image and text 60% opaque
lnopaqueratio = .60
loclrmatrix.matrix33 = lnopaqueratio
loattr.setcolormatrix(loclrmatrix)
lorect.x = 0
lorect.y = ( lomainbmp.
height / 5 * 3) - lologobmp.heightlocolor.a = lnopaqueratio * 255
lobrush.
color = locolor

logfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.
pixel, loattr)
logfx.drawstring("gdiplusx powered", lofont, lobrush, lnxstring, lorect.y)

 
* step 4
* draw image and text 40% opaque
lnopaqueratio = .40
loclrmatrix.matrix33 = lnopaqueratio
loattr.setcolormatrix(loclrmatrix)
lorect.x = 0
lorect.y = ( lomainbmp.
height / 5 * 4) - lologobmp.heightlocolor.a = lnopaqueratio * 255
lobrush.
color = locolor

logfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.
pixel, loattr)
logfx.drawstring("gdiplusx powered", lofont, lobrush, lnxstring, lorect.y)

 
* step 5
* draw image and text 20% opaque
lnopaqueratio = .20
loclrmatrix.matrix33 = lnopaqueratio
loattr.setcolormatrix(loclrmatrix)
lorect.x = 0
lorect.y = ( lomainbmp.
height / 5 * 5) - lologobmp.heightlocolor.a = lnopaqueratio * 255
lobrush.
color = locolor

logfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.
pixel, loattr)
logfx.drawstring("gdiplusx powered", lofont, lobrush, lnxstring, lorect.y)

 
* finished drawing, now save the image and show it !lomainbmp.save("c:\logo9.jpg", .imaging.imageformat.jpeg)run /n explorer.exe c:\logo9.jpgendwith
return

2007-07-17

Watermark your reports with GdiPlusX

here's a reportlistener that generates watermark pictures in reports.


very simple to setup, just set some properties, like the logo image, width and height ratio, transparency ratio.


 


do locfile("system.app")


local lolistener as reportlistener
lolistener = createobject
("watermarklistener")
lolistener.
listenertype
= 1
lolistener.watermarkimage =
addbs(home
()) + "graphics\gifs\morphfox.gif"
lolistener.watermarktype = 2
&& 1 = colored ; 2 = greyscale
lolistener.watermarktransparency = 0.25
&& 0 = transparent ; 1 = opaque
lolistener.watermarkwidthratio = 0.75
&& 0 - 1
lolistener.watermarkheightratio = 0.75
&& 0 - 1


* run the report using the new report engine (object-assisted output)
report form (addbs(home()) + "samples/solution/europa/employeesmd.frx") object
lolistener
return


 


below are some samples generated by the watermarklistener class. (source code at the end of this post)


 


important
requires vfp9 and gdiplusx to run. 
please make sure that you have the latest version!
http://www.codeplex.com/vfpx/wiki/view.aspx?title=gdiplusx&referringtitle=home

 


1 -


      watermarktype = 1 && 1 = colored ; 2 = greyscale
      watermarktransparency = 1 && 0 = transparent ; 1 = opaque
      watermarkwidthratio = 0.90 && 0 - 1
      watermarkheightratio = 0.90 && 0 - 1



 


2 -


      watermarktype = 1 && 1 = colored ; 2 = greyscale
      watermarktransparency = 0.25 && 0 = transparent ; 1 = opaque
      watermarkwidthratio = 0.50 && 0 - 1
      watermarkheightratio = 0.50 && 0 - 1





 


3 -


      watermarktype = 2 && 1 = colored ; 2 = greyscale
      watermarktransparency = 0.10 && 0 = transparent ; 1 = opaque
      watermarkwidthratio = 1 && 0 - 1
      watermarkheightratio = 1 && 0 - 1







 


4 -


      watermarktype = 2 && 1 = colored ; 2 = greyscale
      watermarktransparency = 25 && 0 = transparent ; 1 = opaque
      watermarkwidthratio = 0.75 && 0 - 1
      watermarkheightratio = 0.75 && 0 - 1




 


 


 


do locfile("system.app")


local lolistener as reportlistener
lolistener = createobject("watermarklistener")
lolistener.
listenertype = 1
lolistener.watermarkimage =
addbs(home()) + "graphics\gifs\morphfox.gif"
lolistener.watermarktype = 2
&& 1 = colored ; 2 = greyscale
lolistener.watermarktransparency = 0.25 && 0 = transparent ; 1 = opaque
lolistener.watermarkwidthratio = 0.75 && 0 - 1
lolistener.watermarkheightratio = 0.75 && 0 - 1


* run the report using the new report engine (object-assisted output)
report form home() + "samples/solution/europa/employeesmd.frx") object lolistener
return
 



define class
watermarklistener as _reportlistener of home() + "ffc\" + "_reportlistener.vcx"


newpage = .t.
ogdigraphics =
null
watermarkimage = ""
watermarktype = 1
&& 1 = colored ; 2 = greyscale
watermarktransparency = 0.50 && 0 = transparent ; 1 = opaque
watermarkwidthratio = 0.50
watermarkheightratio = 0.50

function beforereport
   dodefault
()
   this.ogdigraphics = _screen.system.drawing.graphics.new()
endfunc


function beforeband(nbandobjcode, nfrxrecno)
   #
define frx_objcod_pageheader 1
   if nbandobjcode==frx_objcod_pageheader
      this.newpage = .t.
      if not this.issuccessor
         this.sharedgdiplusgraphics = this.gdiplusgraphics
      endif
      this
.ogdigraphics.handle = this.sharedgdiplusgraphics
   endif
   dodefault
(nbandobjcode, nfrxrecno)
endfunc


procedure render(nfrxrecno,;
      nleft,ntop,nwidth,nheight,;
      nobjectcontinuationtype, ;
      ccontentstoberendered, gdiplusimage)


   with _screen.system.drawing
      if this.newpage
         local lnx, lny, lnwidth, lnheight
         lnx = (1 -
this.watermarkwidthratio) / 2
         lny = (1 -
this.watermarkheightratio) / 2
         lnwidth =
this.watermarkwidthratio
         lnheight =
this.watermarkheightratio


         * create a rectangle of the size of 60% of the report page
         local lorect as xfcrectangle
         lorect = .rectangle.new(lnx *
this.sharedpagewidth, ;
            lny *
this.sharedpageheight, ;
            this.sharedpagewidth * lnwidth, ;
            this.sharedpageheight * lnheight)


         * load the image file to gdi+
         local lobmp as xfcbitmap
         lobmp = .
bitmap.new(this.watermarkimage)
 
         local loclrmatrix as xfccolormatrix

         if this.watermarktype = 2 && 1 = colored ; 2 = greyscale
            loclrmatrix = .imaging.colormatrix.new( ; 
               .33, .33, .33, 0 , 0, ; 
               .33, .33, .33, 0 , 0, ; 
               .33, .33, .33, 0 , 0, ;
               0, 0, 0,
this.watermarktransparency, 0, ; 
               0, 0, 0, 0, 0)
         else
            loclrmatrix = .imaging.colormatrix.new()
            loclrmatrix.matrix33 =
this.watermarktransparency
         endif


         local loattr as xfcimageattributes
         loattr = .imaging.imageattributes.new() 
         loattr.setcolormatrix(loclrmatrix)


         this.ogdigraphics.drawimage(lobmp, lorect, lobmp.getbounds(), 2, loattr)


         this.newpage = .f.
      endif
   endwith


   dodefault(nfrxrecno,;
         nleft,ntop,nwidth,nheight,;
         nobjectcontinuationtype, ;
         ccontentstoberendered, gdiplusimage)
endproc


enddefine

2007-07-13

Convert BMP to ICON - Part 1

it is known that gdi+ does not bring full support to icon files. so, we can't make some simple conversions to create .ico files directly.


but there are some easy tricks that we can use to do that. the main step is to retrieve a icon handle from our bitmap, the "hicon".


the simplest aproach is to use the olecreatepictureindirect api to obtain a ole picture object reference. next step is to send this object to the savepicture() function to save it to disk.


but the problem of this technique is that the icons are generated at a lousy quality, 4bpp ( 4 bits per pixel ). this means that the se icons can have at most 2^4 = 16 colors, that brings really ugly and bad quality results. 


 


below is the code:


 

important
requires vfp9 and gdiplusx to run. 
please make sure that you have the latest version!
http://www.codeplex.com/vfpx/wiki/view.aspx?title=gdiplusx&referringtitle=home

 


* api declarations
declare short destroyicon in user32 integer hicon
declare long olecreatepictureindirect in oleaut32 ;
   string @pictdesc , string @riid , long own , object @obj


* initialize gdiplusx
_screen.addproperty("system", newobject("xfcsystem", locfile("system.vcx")))


local lcpict, lciconfile
lcpict =
getpict("bmp")
lciconfile = "c:\" +
juststem(lcpict) + ".ico"


with _screen.system.drawing
   local lhicon
   local lobmp as xfcbitmap
   lobmp = .
bitmap.fromfile(lcpict)
   lhicon = lobmp.gethicon()
endwith
 
if
lhicon # 0 
   *!* typedef struct tagpictdesc 
   *!* { 
   *!* uint cbsizeofstruct; 
   *!* uint pictype; 
   *!* hicon hicon; 
   *!* } icon; 
   *!* struct 
   *!* { 
   *!* henhmetafile hemf; 
   *!* } emf; 
   *!* } ; 
   *!* } pictdesc;


   #define pictype_icon 3
   #
define guid_icon2 0h0004020000000000c000000000000046 && "{00020400-0000-0000-c000-000000000046}"
   #define guid_icon 0h8109f87b32bf1a108bbb00aa00300cab && "{7bf80981-bf32-101a-8bbb-00aa00300cab}"


   local lcpictdesc, lqguid, loiconobj
   lcpictdesc =
bintoc(16,"4rs") + ; && size of structure
      bintoc(pictype_icon, "4rs") + ; && type of image
      bintoc(lhicon, "4rs") + ; && image handle
      bintoc(0, "4rs")


   lqguid = guid_icon
   loiconobj = 0 

   *!* http://msdn2.microsoft.com/en-us/library/ms694511.aspx
   *!* stdapi olecreatepictureindirect( 
   *!* pictdesc* ppictdesc, //pointer to the structure of parameters for picture
   *!* refiid riid, //reference to the identifier of the interface
   *!* bool fown, //whether the picture is to be destroyed
   *!* if true, the picture object is to destroy its picture when the object is destroyed. if false, the caller is responsible for destroying the picture.
   *!* void** ppvobj //address of output variable that receives the 
   *!* // interface pointer requested in riid
   *!* );


   * create the picture ole object
   olecreatepictureindirect(@lcpictdesc, @lqguid, 1, @loiconobj)


   if vartype(loiconobj) = 'o'
      * save the picture using vfp native function 'savepicture()' 
      if savepicture(loiconobj, lciconfile) 
         messagebox('icon created successfully !', 64) 
      endif 
   else 
   messagebox
('olecreatepictureindirect() error', 16)
endif

* free the memory handle allocated for the hicon
= destroyicon(lhicon)
endif
return



 


















original picture .bmp       icon picture .ico (16 colors)
      
      
      
      



 


you can try it by yourself without running this code: open mspaint, and load one of the four images on the left. then, select to save the image as a 16 color bitmap, and you'll obtain exactly the same result that olecreatepictureindirect generated. it will use only the color palette below to convert the image.



so, as the result isn't as good as desired, why did i post these codes here ?


that's because the original .net "system.drawing" library uses a similar aproach as the shown above to create icons, in the icon.save() method.


in the next post i'll show how we can convert our bitmaps to high quality icons, but using some good and old gdi ( not gdi+) techniques. the code wont be simple and short as the shown here, but the performance will be similar and the quality will be perfect !

2007-07-11

Draw logos in your images with GdiPlusX

Another common request that I always find on some VFP forums is people wanting to draw some images, usually company logos over some pictures. this is really simple, as i'll show below.

For all the samples that i'll provide the vfpx logo will be drawn in some bigger pictures. to show all the flexibility that gdi+ can offer to us, some effects will be aplied to the logo.



ImportantRequires VFP9 and the Gdiplusx library to run.  

Please make sure that you have the latest version!
https://github.com/VFPX/GDIPlusX

sample 1: draw image without transformation



do locfile("system.app")

with _screen.system.drawing as xfcdrawing

local lcmainpict, lclogopictlocal lomainbmp as xfcbitmaplocal lologobmp as xfcbitmaplocal logfx as xfcgraphics

lcmainpict = getpict()
lclogopict =
getpict()

lomainbmp = .bitmap.fromfile(lcmainpict)
lologobmp = .
bitmap.fromfile(lclogopict)
logfx = .graphics.fromimage(lomainbmp)


*!* example 1
*!* original image
*!* position : top left
*!* draw logo without any transformation
logfx.drawimage(lologobmp, 0, 0)
lomainbmp.
save("c:\logo1.jpg", .imaging.imageformat.jpeg)

* show imagerun /n explorer.exe c:\logo1.jpgendwith



sample 2 : convert the white background of our logo to transparent, using the bitmap.maketransparent() function



do locfile("system.app")

with _screen.system.drawing as xfcdrawing

local lcmainpict, lclogopictlocal lomainbmp as xfcbitmaplocal lologobmp as xfcbitmaplocal logfx as xfcgraphics

lcmainpict = getpict()
lclogopict =
getpict()


lomainbmp = .bitmap.fromfile(lcmainpict)
lologobmp = .
bitmap.fromfile(lclogopict)
logfx = .graphics.fromimage(lomainbmp) 
*!* sample 2
*!* converted selected color from logo with alpha 255 (opaque) to transparent alpha (0)
*!* position : top right
* force the white background from the logo to become transparent
lologobmp.maketransparent(.color.white)


* draw logo at the top right position of imagelocal x1, y1
x1 = lomainbmp.
width - lologobmp.widthy1 = 0
logfx.drawimage(lologobmp, x1, y1)
lomainbmp.
save("c:\logo2.jpg", .imaging.imageformat.jpeg)


run /n explorer.exe c:\logo2.jpgendwith



sample 3 : draw the logo aplying 25% transparency to the whole image.

the transparency is aplied to the whole image using a colormatrix. the transparency ratio that is aplied ranges from 0 (totally transparent) to 1 (totally opaque).


 
do locfile("system.app")

with _screen.system.drawing as xfcdrawing

local lcmainpict, lclogopictlocal lomainbmp as xfcbitmaplocal lologobmp as xfcbitmaplocal logfx as xfcgraphics

lcmainpict = getpict()
lclogopict =
getpict()

lomainbmp = .bitmap.fromfile(lcmainpict)
lologobmp = .
bitmap.fromfile(lclogopict)
logfx = .graphics.fromimage(lomainbmp)


*!* sample 3
*!* draw the logo aplying 25% transparency to the whole image
*!* position: bottom left
* the transparency is aplied to the whole image
* define the transparency ratio that will be aplied
* this parameter ranges from 0 (totally transparent) to 1 (totally opaque)
local lntranspratio
lntranspratio = 0.25
&& 25%

* create a colormatrix that will have the transformations information
* the position (4,4) of the matrix is responsible for the opacity
local loclrmatrix as xfccolormatrix
loclrmatrix = .imaging.colormatrix.new( ;
   1, 0, 0, 0 , 0, ;
   0, 1, 0, 0 , 0, ;
   0, 0, 1, 0 , 0, ;
   0, 0, 0, lntranspratio, 0, ;
   0, 0, 0, 0 , 0)


* create an image attributes object to create the effects based in our clrmatrixlocal loattr as xfcimageattributes
loattr = .imaging.imageattributes.new()
loattr.setcolormatrix(loclrmatrix)


* we need to create a rectangle that will contain the coordinates and size of the transformed logolocal lorect as xfcrectangle
lorect = .rectangle.new()
lorect.x = 0
lorect.y = lomainbmp.
height - lologobmp.heightlorect.width = lologobmp.widthlorect.height = lologobmp.height * draw the transformed image using the rectangle and imgattributes/clrmatrixlogfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.pixel, loattr)
lomainbmp.
save("c:\logo3.jpg", .imaging.imageformat.jpeg)

run /n explorer.exe c:\logo3.jpgendwith 


sample 4 : draw the logo with transparent background with 50% global transparency.
aply 100% transparency to the white color, to eliminate the background
draw the logo aplying 50% transparency to the whole image, similar to the previous sample.

 


do locfile("system.app")

with _screen.system.drawing as xfcdrawing

local lcmainpict, lclogopictlocal lomainbmp as xfcbitmaplocal lologobmp as xfcbitmaplocal logfx as xfcgraphics

lcmainpict = getpict()
lclogopict =
getpict()
lomainbmp = .
bitmap.fromfile(lcmainpict)
lologobmp = .
bitmap.fromfile(lclogopict)
logfx = .graphics.fromimage(lomainbmp)
 
*!* sample 4
*!* aply 100% transparency to the white color, to eliminate the background
*!* draw the logo aplying 50% transparency to the whole image
*!* position: top left
* first step: eliminate the white background
lologobmp.maketransparent(.color.white)

* define the transparency ratio that will be aplied
* this parameter ranges from 0 (totally transparent) to 1 (totally opaque)
local lntranspratio
lntranspratio = 0.50
&& 50%

* create a colormatrix that will have the transformations information
* the position (4,4) of the matrix is responsible for the opacity
local loclrmatrix as xfccolormatrix
loclrmatrix = .imaging.colormatrix.new( ;
   1, 0, 0, 0 , 0, ;
   0, 1, 0, 0 , 0, ;
   0, 0, 1, 0 , 0, ;
   0, 0, 0, lntranspratio, 0, ;
   0, 0, 0, 0 , 0)


* create an image attributes object to create the effects based in our clrmatrixlocal loattr as xfcimageattributes
loattr = .imaging.imageattributes.new()
loattr.setcolormatrix(loclrmatrix)


* we need to create a rectangle that will contain the coordinates and size of the transformed logolocal lorect as xfcrectangle
lorect = .rectangle.new()
lorect.x = 0
lorect.y = 0
lorect.
width = lologobmp.widthlorect.height = lologobmp.height * draw the transformed image using the rectangle and imgattributes/clrmatrixlogfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.pixel, loattr)
lomainbmp.
save("c:\logo4.jpg", .imaging.imageformat.jpeg)

run /n explorer.exe c:\logo4.jpgendwith