In This Topic
Programming / Image Processing & Editing / Adding a watermark to your documents

Adding a watermark to your documents

In This Topic

Adding a logo or a watermark on the background of your documents, which cannot be erased, is simple in GdPicture. All you need is an image of the watermark and your source document. Here is an example.

Copy Code
'We assume that GdPicture has been correctly installed and unlocked.

Dim oGdPictureImaging As New GdPictureImaging()

'Loading the document as an image from the file.

Dim imageId As Integer = oGdPictureImaging.CreateGdPictureImageFromFile("input.tif")

If oGdPictureImaging.GetStat() <> GdPictureStatus.OK Then

    MessageBox.Show("The file can't be loaded. Error: " + oGdPictureImaging.GetStat().ToString(), "Watermark Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

Else

    'Loading the watermark image from the file.

    Dim waterImage As Integer = oGdPictureImaging.CreateGdPictureImageFromFile("WaterMark.tif")

    If oGdPictureImaging.GetStat() = GdPictureStatus.OK Then

       'We assume the logo or text is in black and the background of the watermark image is white.

        'Then we set the background of the watermark image to be transparent.

        Dim status As GdPictureStatus = oGdPictureImaging.SetTransparencyColor(waterImage, oGdPictureImaging.ARGB(255, 255, 255, 255))

        If status <> GdPictureStatus.OK Then

            MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString(), "Watermark Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Else

            'Now you can combine the two images onto the document.

            status = oGdPictureImaging.DrawGdPictureImageTransparency(waterImage, imageId, 160, 100, 100, 200, 200, System.Drawing.Drawing2D.InterpolationMode.Bicubic)

            If status <> GdPictureStatus.OK Then

                MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString(), "Watermark Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

            Else

                'Converting the document image to greyscale:

                oGdPictureImaging.ConvertTo8BppGrayScale(imageId)

                'Saving the resulting image with added watermark.

                status = oGdPictureImaging.SaveAsTIFF(imageId, "output.tif", TiffCompression.TiffCompressionAUTO)

                If status <> GdPictureStatus.OK Then

                    MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString(), "Watermark Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

        oGdPictureImaging.ReleaseGdPictureImage(waterImage)

    Else

        MessageBox.Show("The watermark image can't be loaded. Error: " + oGdPictureImaging.GetStat().ToString(), "Watermark Example", MessageBoxButtons.OK, MessageBoxIcon.Error)

    End If

    oGdPictureImaging.ReleaseGdPictureImage(imageId)

End If

oGdPictureImaging.Dispose()
Copy Code
//We assume that GdPicture has been correctly installed and unlocked.

GdPictureImaging oGdPictureImaging = new GdPictureImaging();

//Loading the document as an image from the file.

int imageId = oGdPictureImaging.CreateGdPictureImageFromFile("input.tif");

if (oGdPictureImaging.GetStat() != GdPictureStatus.OK)

{

    MessageBox.Show("The file can't be loaded. Error: " + oGdPictureImaging.GetStat().ToString(), "Watermark Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

else

{

    //Loading the watermark image from the file.

    int waterImage = oGdPictureImaging.CreateGdPictureImageFromFile("WaterMark.tif");

    if (oGdPictureImaging.GetStat() == GdPictureStatus.OK)

    {

        //We assume the logo or text is in black and the background of the watermark image is white.

        //Then we set the background of the watermark image to be transparent.

        GdPictureStatus status = oGdPictureImaging.SetTransparencyColor(waterImage, oGdPictureImaging.ARGB(255, 255, 255, 255));

        if (status != GdPictureStatus.OK)

        {

            MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString(), "Watermark Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        else

        {

            //Now you can combine the two images onto the document.

            status = oGdPictureImaging.DrawGdPictureImageTransparency(waterImage, imageId, 160, 100, 100, 200, 200, System.Drawing.Drawing2D.InterpolationMode.Bicubic);

            if (status != GdPictureStatus.OK)

            {

                MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString(), "Watermark Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            else

            {

                //Converting the document image to greyscale:

                oGdPictureImaging.ConvertTo8BppGrayScale(imageId);

                //Saving the resulting image with added watermark.

                status = oGdPictureImaging.SaveAsTIFF(imageId, "output.tif", TiffCompression.TiffCompressionAUTO);

                if (status != GdPictureStatus.OK)

                {

                    MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString(), "Watermark Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

            }

        }

        oGdPictureImaging.ReleaseGdPictureImage(waterImage);

    }

    else

    {

        MessageBox.Show("The watermark image can't be loaded. Error: " + oGdPictureImaging.GetStat().ToString(), "Watermark Example", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    oGdPictureImaging.ReleaseGdPictureImage(imageId);

}

oGdPictureImaging.Dispose();