25 de junio de 2015

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

Artículo original: Draw Logos in your images with GdiPlusX - Part 2
http://weblogs.foxite.com/vfpimaging/archive/2007/07/27/4338.aspx
Autor: Cesar Ch.
Traducido por: Luis Maria Guayán


Aquí están 5 nuevos ejemplos derivados de otros que mostré en un artículo anterior. (Nota del traductor: la traducción de este artículo está en este Blog)

Para todos los ejemplos que proporcionaré, el logo de VFPX será dibujado en algunas imágenes mas grandes. Para demostrar toda la flexibilidad que GDI+ puede ofrecernos, algunos efectos serán aplicados al logo.

IMPORTANTE

Requiere VFP9 y GdiPlusX para funcionar.

¡Por favor asegúrese que tiene la última versión!

http://www.codeplex.com/VFPX/Wiki/View.aspx?title=GDIPlusX&referringTitle=Home

EJEMPLO 5:

Aplico 100% de transparencia al color blanco para eliminar el fondo. Dibujo el logo aplicando la matriz de color definida con la siguiente transformación: convierto a escala de grises con 50% de transparencia a toda la imagen.
Posición: Borde superior izquierdo.

_SCREEN.AddProperty("System", NEWOBJECT("xfcSystem", LOCFILE("system.vcx"))) 
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 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 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( ; 
     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 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:\logo5.jpg", .Imaging.ImageFormat.Jpeg)
  RUN /N explorer.exe c:\logo5.jpg
ENDWITH 
RETURN

Ejemplo 6:

Aplico 100% de transparencia al color blanco para eliminar el fondo. Dibujo el logo aplicando la matriz de color definida con la siguiente transformación: convierto a escala de grises con 50% de transparencia a toda la imagen.
Posición: Centro.
Tamaño: Agrandada 4 veces

_SCREEN.AddProperty("System", NEWOBJECT("xfcSystem", LOCFILE("system.vcx")))

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 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 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.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 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 = (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/ClrMatrix
  loGfx.DrawImage(loLogoBmp, loRect, loLogoBmp.GetBounds(), .GraphicsUnit.Pixel, loAttr) 
  loMainBmp.Save("c:\logo6.jpg", .Imaging.ImageFormat.Jpeg)

  RUN /N explorer.exe c:\logo6.jpg
ENDWITH 
RETURN

Ejemplo 7:

Aplico 100% de transparencia al color blanco para eliminar el fondo. Dibujo el logo aplicando 25% de opacidad a toda la imagen.
Posición: Centro.
Tamaño: Agrandada 4 veces

_SCREEN.AddProperty("System", NEWOBJECT("xfcSystem", LOCFILE("system.vcx")))

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 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 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.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 = (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/ClrMatrix
  loGfx.DrawImage(loLogoBmp, loRect, loLogoBmp.GetBounds(), .GraphicsUnit.Pixel, loAttr) 
  loMainBmp.Save("c:\logo7.jpg", .Imaging.ImageFormat.Jpeg)
  RUN /N explorer.exe c:\logo7.jpg
ENDWITH 
RETURN

Ejemplo 8:

Usando la tecnica mostrada anteriormente, dibujo logos y textos aplicando transparencias variables.

El siguiente código no está optimizado, y solo intenta mostrar algunas posibilidades.

_SCREEN.AddProperty("System", NEWOBJECT("xfcSystem", LOCFILE("system.vcx")))

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 8
  *!* Draw image and text in different transparencies
  LOCAL lcString
  LOCAL loFont as xfcFont
  loFont = .Font.New("Verdana", 22, .FontStyle.BoldItalic)
  LOCAL loColor as xfcColor
  loColor = .Color.White
  LOCAL lnXString
  lnXString = 0 + loLogoBmp.Width
 
  * 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

  * 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 ClrMatrix
  LOCAL loAttr AS xfcImageAttributes
  loAttr = .Imaging.ImageAttributes.New() 

  * 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.Width = loLogoBmp.Width
  loRect.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.Height

  loGfx.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.Height
  loColor.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.Height

  loColor.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.Height

  loColor.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.Height

  loColor.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.jpg
ENDWITH 
RETURN

Ejemplo 9:

Como en los ejemplos anteriores, usando las técnicas ya vistas, dibujo logos y texto aplicando transparencias variables, usando un logo monocromático.

El siguiente código no está optimizado, y solo intenta mostrar algunas posibilidades.

_SCREEN.AddProperty("System", NEWOBJECT("xfcSystem", LOCFILE("system.vcx")))

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 9
  *!* Draw image and text in different transparencies
  LOCAL lcString
  LOCAL loFont as xfcFont
  loFont = .Font.New("Verdana", 30, .FontStyle.BoldItalic)
  LOCAL loColor as xfcColor
  loColor = .Color.White
  LOCAL loBrush as xfcSolidBrush
  loBrush = .SolidBrush.New(loColor)
 
  LOCAL lnXString
  lnXString = 0 + 10 + loLogoBmp.Width
 
  * 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
  * 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 ClrMatrix
  LOCAL loAttr AS xfcImageAttributes
  loAttr = .Imaging.ImageAttributes.New() 
  * 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.Width = loLogoBmp.Width
  loRect.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.Height

  loGfx.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.Height
  loColor.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.Height

  loColor.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.Height

  loColor.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.Height

  loColor.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.jpg
ENDWITH 
RETURN

No hay comentarios. :

Publicar un comentario

Los comentarios son moderados, por lo que pueden demorar varias horas para su publicación.