Foxite.COM Community Weblog

Foxite.COM Community Weblog - free weblog service for the Visual FoxPro Community.
Welcome to Foxite.COM Community Weblog Sign in | Join | Help
in
Home Blogs Forum Photos Forum Archives

VFP IMAGING



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

Published Wednesday, July 11, 2007 4:36 AM by cesarchalom
Filed Under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Simon Arnold said:

Absolutley fantastic, keep up the good work.
July 11, 2007 11:31 AM
 

Franklin Garzón said:

Excelente, muy buen resultado, saludos.
July 11, 2007 8:10 PM
 

LuisMaria said:

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
July 26, 2007 5:56 AM
 

LuisMaria said:

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
July 28, 2007 3:34 AM
 

Cesar Chalom said:

Here are 5 new samples derived from the ones that I showed in a previous post.
 
For all the samples that...
July 28, 2007 4:43 AM

What do you think?

(required) 
(optional)
(required)