在使用各种文档时,经常需要为文档添加一些水印或者公司的标志作为文档的背景达到保护文档的作用,水印也不会轻易被擦除。
GdPicture.NET图片文档处理控件可以帮助开发人员轻松实现该功能,通过该控件提供的API可以为文档插入水印,设置水印的位置、大小以及透明度等,具体可以参考下面的代码:
//We assume that GdPicture has been correctly installed and unlocked.
GdPictureImaging oGdPictureImaging = new GdPictureImaging();
//Loading the Document Image from file
int imageId = oGdPictureImaging.CreateGdPictureImageFromFile("input.tif");
if (imageId == 0)
{
MessageBox.Show("Image Could Not Be Loaded! " + oGdPictureImaging.GetStat().ToString());
}
else
{
//Loading the watermark image from file
int waterImage = oGdPictureImaging.CreateGdPictureImageFromFile("WaterMark.tif");
if (waterImage != 0)
{
//We assume the logo or text is in black and the background of the watermark image is white
//We then set the background of the watermark image to transparent
GdPictureStatus status = oGdPictureImaging.SetTransparencyColor(waterImage, oGdPictureImaging.ARGB(255, 255, 255, 255));
if (status != GdPictureStatus.OK)
{
MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString());
}
else
{
//Now you can combine the two images onto the document
status = oGdPictureImaging.DrawGdPictureImageTransparency(waterImage, imageId, 160, 100, 100, 200, 200,
Drawing2D.InterpolationMode.Bicubic);
if (status != GdPictureStatus.OK)
{
MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString());
}
else
{
//Convert the document image to greyscale:
oGdPictureImaging.ConvertTo8BppGrayScale(m_CurrentImage);
//Save the image
status = oGdPictureImaging.SaveAsTIFF(imageId, "output.tif", TiffCompression.TiffCompressionAUTO);
if (status != GdPictureStatus.OK)
{
MessageBox.Show("Error: " + oGdPictureImaging.GetStat().ToString());
}
}
}
oGdPictureImaging.ReleaseGdPictureImage(waterImage);
}
else
{
MessageBox.Show("Watermark Image Could Not Be Loaded! " + oGdPictureImaging.GetStat().ToString());
}
oGdPictureImaging.ReleaseGdPictureImage(imageId);
}