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.




 

 

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

 


sample 1: draw image without transformation



do locfile("system.app")


with _screen.system.drawing as xfcdrawing


local lcmainpict, lclogopict
local lomainbmp as xfcbitmap
local lologobmp as xfcbitmap
local 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 image
run /n explorer.exe c:\logo1.jpg
endwith


 


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, lclogopict
local lomainbmp as xfcbitmap
local lologobmp as xfcbitmap
local 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 image
local x1, y1
x1 = lomainbmp.
width - lologobmp.width
y1 = 0
logfx.drawimage(lologobmp, x1, y1)
lomainbmp.
save("c:\logo2.jpg", .imaging.imageformat.jpeg)


run /n explorer.exe c:\logo2.jpg
endwith


 


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, lclogopict
local lomainbmp as xfcbitmap
local lologobmp as xfcbitmap
local 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 clrmatrix
local 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 logo
local lorect as xfcrectangle
lorect = .rectangle.new()
lorect.x = 0
lorect.y = lomainbmp.
height - lologobmp.height
lorect.width = lologobmp.width
lorect.height = lologobmp.height

* draw the transformed image using the rectangle and imgattributes/clrmatrix
logfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.pixel, loattr)
lomainbmp.
save("c:\logo3.jpg", .imaging.imageformat.jpeg)


run /n explorer.exe c:\logo3.jpg
endwith
 



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, lclogopict
local lomainbmp as xfcbitmap
local lologobmp as xfcbitmap
local 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 clrmatrix
local 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 logo
local lorect as xfcrectangle
lorect = .rectangle.new()
lorect.x = 0
lorect.y = 0
lorect.
width = lologobmp.width
lorect.height = lologobmp.height

* draw the transformed image using the rectangle and imgattributes/clrmatrix
logfx.drawimage(lologobmp, lorect, lologobmp.getbounds(), .graphicsunit.pixel, loattr)
lomainbmp.
save("c:\logo4.jpg", .imaging.imageformat.jpeg)


run /n explorer.exe c:\logo4.jpg
endwith

6 comments:

  1. Absolutley fantastic, keep up the good work.

    ReplyDelete
  2. Excelente, muy buen resultado, saludos.

    ReplyDelete
  3. Este artículo esta traducido al español en PortalFox en:


    -- Dibujar Logos en sus imágenes con GdiPlusX --

    http://www.portalfox.com/article.php?sid=2479

    ReplyDelete
  4. Este articulo estra traducido al español en PortalFox en:


    -- Dibujar Logos en sus imágenes con GdiPlusX --

    http://www.portalfox.com/article.php?sid=2479

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

     

    For all the samples that...

    ReplyDelete
  6. Hola Cesar,


    Acabo de hacer un copiado de la pantalla, pero ahora quiero colocarle una marca de agua, me podrias indicar como hacerlo, por que ya llevo varias horas sin tener exito. :(

    Mi codigo es:


    DO LOCFILE("System.App")

    LOCAL loCaptureBmp AS xfcBitmap

    WITH _Screen.System.Drawing

      loCaptureBmp = .Bitmap.FromScreen()

      loCaptureBmp.ToClipboard()

    ENDWITH


    Gracias por la ayuda

    ReplyDelete