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

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