Example of Watermarking Images in ASP With csImageFile

csImageFile can use the alpha transparency of an existing PNG image to produce a smooth watermark. The following example shows how to add a watermark to a photograph using a PNG that was prepared earlier.

The two images used are shown below. Note that the watermark image is a PNG containing an alpha channel and your browser might not display this correctly.



Example 1 - With Alpha Transparency

Set Image = Server.CreateObject("csImageFile.Manage")
Image.ReadFile "c:\images\background.jpg"
Image.MergeBack "c:\images\watermark.png", 0, 0
Response.ContentType = "image/jpeg"
Response.BinaryWrite Image.JPGData

PNG image alpha transparency in ASP

The background photograph is loaded into csImageFile using ReadFile and the MergeBack command is used to merge the watermark image onto the photograph. The transparency details are detected automatically.

Most of the watermark image is completely transparent and the text is partially transparent.

It would also have been possible to load the background first and then use MergeFront to load the watermark.

Example 2 - Merging Without Alpha Transparency

This shows the result when the same images are merged but with the alpha transparency removed first.

Set Image = Server.CreateObject("csImageFile.Manage")
Image.ReadFile "c:\images\watermark.png"
Image.TransparentColor = "ffffff"
Image.Transparent = true
Image.MergeFront "c:\images\background.jpg", 0, 0
Response.ContentType = "image/jpeg"
Response.BinaryWrite Image.JPGData

This time the watermark is loaded into csImageFile using ReadFile and the MergeFront command is used to load the background image. The alpha channel is cleared, TransparentColor is set to white and Transparent is set to true. This removes the plain background from the watermark image, but the text is completely opaque.

Support for alpha transparency has been introduced in version 6.2 of csImageFile and it is limited to PNG images. Further functionality was added for version 6.4.7. allowing the alpha channel to be exported (or imported) as a separate greyscale image which can be edited. We have an example of using this functionality to store an alpha channel temporarily so it can be restored after a merge operation.


This site uses cookies for functionality, traffic analysis and for targeted advertising. Click the Accept button to accept our Cookie Policy. The Cookie Policy page offers configuration for a reduced set of cookies for this site.