Examples of Merging Images in ASP With csImageFile

csImageFile has 6 merging commands which merge the currently loaded image with a second image, taken from a file, binary data or a Windows handle. The currently loaded image can go to the back (MergeBack) or it can go to the front (MergeFront). A single colour can be made transparent and the entire foreground image can be given a percentage transparency to fade it into the background. These transparency options will be demonstrated in the examples below.

For this demonstration we have produced two images, a letter and a postage stamp. The merge operation will stick the stamp on the letter.

ASP image merging - background image

Letter

ASP image merging - foreground image

Stamp

Example 1 - No Transparency

Set Image = Server.CreateObject("csImageFile.Manage")
Image.ReadFile "c:\images\letter.gif"
Image.MergeBack "c:\images\stamp.gif", 230, 20
Response.ContentType = "image/gif"
Response.BinaryWrite Image.GIFData

ASP image merging - no transparency

The letter is loaded into csImageFile using ReadFile and the MergeBack command is used to stick the second image, the stamp, onto the letter. The coordinates (230, 20) specify the top left corner of the stamp on the original image.

There is no transparency so no part of the letter shows through where the stamp covers it.

It would also have been possible to load the stamp first and then use MergeFront to stick it on the letter.

Example 2 - Single Colour Transparency

Set Image = Server.CreateObject("csImageFile.Manage")
Image.ReadFile "c:\images\letter.gif"
Image.Transparent = true
Image.TransparentColor = "FF0000"
Image.MergeBack "c:\images\stamp.gif", 230, 20
Response.ContentType = "image/gif"
Response.BinaryWrite Image.GIFData

ASP image merging - single colour transparency

The letter is loaded into csImageFile using ReadFile and the MergeBack command is used to stick the second image, the stamp, onto the letter as before. This time TransparentColor is set to the red background colour of the stamp and Transparent is set to true. The colour of the letter shows through the red part of the stamp. It is important to note that only one RGB value can be made transparent and so a JPEG would not make a suitable foreground image, because JPEGs do not have continuous coloured areas.

Example 3 - Percentage Transparency

Set Image = Server.CreateObject("csImageFile.Manage")
Image.ReadFile "c:\images\letter.gif"
Image.Transparent = true
Image.TransparentColor = "FF0000"
Image.Transpercent = 70
Image.MergeBack "c:\images\stamp.gif", 230, 20
Response.ContentType = "image/gif"
Response.BinaryWrite Image.GIFData

ASP image merging - full and partial transparency

This time the Transpercent property is set to 70, which makes the entire foreground image 70% transparent. Red is still fully transparent because TransparentColor is "FF0000" and Transparent is true. It is also possible to have a percentage transparency while Transparent is set to false.

Example 4 - Joining Images Side by Side

It is not possible to join two smaller images together to make a larger image, but the first image can be made bigger so that the second can be placed on top of it. Alternatively a new image can be created that is big enough for both smaller images to be placed onto it side by side. In this example we will take the stamp we used earlier and stick a similar blue stamp next to it. We will use two instances of the csImageFile object so that we can load both images and find their width. We will use a feature of the Crop command that adds space to the right of an image by cropping to a larger area than the original.

Dim Width1
Set Image1 = Server.CreateObject("csImageFile.Manage")
Set Image2 = Server.CreateObject("csImageFile.Manage")
Image1.ReadFile "c:\images\stamp.gif"
Image2.ReadFile "c:\images\bluestamp.gif"
Width1 = Image1.Width
Image1.Crop 0, 0, Width1 + Image2.Width, Image1.Height
Image1.MergeBackHDC Image2.BMPHandle, Width1, 0
Response.ContentType = "image/gif"
Response.BinaryWrite Image1.GIFData

ASP image edits - increasing the canvas size

The first instance of csImageFile is Image1 and this is going to be used to hold the composite image. We load the first image and store the width in a variable for use later. The Crop command is used to set the width to the total of both images. The MergeBackHDC command is used to place the image from the second object instance onto the first instance. This technique of using two instances of csImageFile allows two images to be manipulated at once and for information from one to be used in the other.

There is an alternative method that will product the same result. A blank image could be created using NewImage that is the correct final size, and then both the smaller images could be added to this.

Cookies

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.