Merging Images and Watermarking in C# using csXImage.ocx

Two of the methods for merging images with csXImage are MergeFile and MergeBinary. These methods each provide the same functionality, enabling a second image to be merged into the image currently loaded in the control. With MergeBinary, the second image is read from a byte array variable. MergeFile, which is demonstrated in the demo C# application, reads the second image from a file on disk.

Several options are available to the user to modify the way that the merge is carried out. These options are best demonstrated by showing the different effects achieved when a simple monochrome image, containing black text on a white background, is merged into a full colour photograph.

The two images being used for this demonstration are shown below. All the following examples were prepared using the C# demo application, with the photograph on the left as the main image (loaded into the csXImage control before using the MergeFile method) and the text image on the right as the second image.

ActiveX load background image

Main image

ActiveX load foreground image

Second (merged) image

First let's look at the effect of allowing transparency in the second image. If a straightforward merge is carried out, with no transparency, the second image will be stamped onto the first image, hiding all the pixels of the first image covered by the rectangle of the second image. This is shown on the left below. If transparency is allowed in the second image, and the transparent colour is specified as white, then only the black text will be merged, with the remaining pixels of the original image retained. This is shown on the right. The equivalent C# code to give these effects is shown beside each image.

VB image without transparency

axImageBox1.MergeStyle =
    csXImageTrial.TxWMStyle.wmCentre;
axImageBox1.MergeTransparent = false;
axImageBox1.MergeFile(mergeFileName);

ActiveX image with white transparent

axImageBox1.MergeStyle =
    csXImageTrial.TxWMStyle.wmCentre;
axImageBox1.MergeTransparent = true;
axImageBox1.MergeTransparentColor =
    System.Drawing.Color.White;
axImageBox1.MergeTransparency = 0;
axImageBox1.MergeFile(mergeFileName);

In the above examples, the first image has been simply stamped onto the main image. Alternatively, it can be made more transparent by setting a value for the MergeTransparency property (0 to 100%). This can give a "watermark" effect. In the images below, a value of 80% has been used so the second image appears lightly blended into the main image.

Four examples are given, showing the four different values for the MergeStyle property. This property determines how the second image is positioned on the main image.

Merge image using ActiveX control

axImageBox1.MergeStyle =
    csXImageTrial.TxWMStyle.wmSingle;
axImageBox1.MergeTransparent = true;
axImageBox1.MergeTransparentColor =
    System.Drawing.Color.White;
axImageBox1.MergeTransparency = 80;
axImageBox1.MergeFile(mergeFileName);

ActiveX tiled watermark image

axImageBox1.MergeStyle =
    csXImageTrial.TxWMStyle.wmTile;
axImageBox1.MergeTransparent = true;
axImageBox1.MergeTransparentColor =
    System.Drawing.Color.White;
axImageBox1.MergeTransparency = 80;
axImageBox1.MergeFile(mergeFileName);

C# image watermark positioned centrally

axImageBox1.MergeStyle =
    csXImageTrial.TxWMStyle.wmCentre;
axImageBox1.MergeTransparent = true;
axImageBox1.MergeTransparentColor =
    System.Drawing.Color.White;
axImageBox1.MergeTransparency = 80;
axImageBox1.MergeFile(mergeFileName);

C# image repeating watermark

axImageBox1.MergeStyle =
    csXImageTrial.TxWMStyle.wmWrap;
axImageBox1.MergeTransparent = true;
axImageBox1.MergeTransparentColor =
    System.Drawing.Color.White;
axImageBox1.MergeTransparency = 80;
axImageBox1.MergeFile(mergeFileName);

In some cases, the image already loaded into the csXImage control is the one that needs to be used as the second image, with the main image being available on disk. Another property, MergeReverse, can be used to specify that the images will be merged in the opposite order.

The image resulting from a merge operation always retains the colour format and dimensions of the main image.

<<Previous page  Next page>>