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



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.

 

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

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

 

 

 

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

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

Published Tuesday, July 17, 2007 6:47 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

 

Koen said:

Great stuff, please keep on inventing.
July 17, 2007 9:28 PM
 

Imran Syed said:

Can we use this water mark technique in forms ?
i wanted to use my company logo in semi transparent in middle of top level form.
when i maximize form logo pic used appears in multiple times.

thanks.

Imran,

Please have a look at my newer posts, there I show how to transform logos in transparent, I'm sure this will help you to get started.

Hope this helps

Cesar

July 21, 2007 11:15 AM
 

LuisMaria said:

Este articulo estra traducido al español en PortalFox en:

-- Marcas de agua en tus informes con GdiPlusX --
http://www.portalfox.com/article.php?sid=2481
July 28, 2007 3:35 AM
 

Ken Rivers said:

This is a great utility - Thanks.  Can you help witha problem.  When printing the report with a Watermark, it only seem to print on every other page.  Can you tell me if there is anything else I should do.

Hi Ken,

I hope to publish a new version of this class soon. Please keep in touch, and tell me how it works !

Thians in advance

Cesar

September 18, 2007 6:25 PM
 

Kevin Baugh said:

Love this!
Any news on the new version?

What is the best way to include all this in an install ??
January 27, 2008 6:03 PM

What do you think?

(required) 
(optional)
(required)