Solved c# Add watermark using tiff compression

Posted on 2012-09-04
Last Modified: 2012-09-24
We are trying to ad a watermark (text) to an existing tiff file, either to all or selected set of pages.

There have been some issues arising when attempting to apply compression to the resulting file. Either it does not compress, and is excessively large compared to the original file size, or fails when served from  Windows 2003. In the case of the snippet below, one method of .Save does not apply the watermark on the output file, and the other produces the oversize file. Any insight appreciated.

protected void SaveWatermark2()
            Font font = new Font("Tahoma", 64, FontStyle.Bold);
            SolidBrush brush = new SolidBrush(Color.FromArgb(60, Color.Gray));

            Stream stream = File.OpenRead(sPath);
            Bitmap oBitmap = new Bitmap(stream);

            Graphics oGraphic;

            Bitmap bmpNew = new Bitmap(oBitmap.Width, oBitmap.Height);
            bmpNew.SelectActiveFrame(FrameDimension.Page, 0);

            oBitmap.SelectActiveFrame(FrameDimension.Page, 0);
            oGraphic = Graphics.FromImage(bmpNew);
            oGraphic.SmoothingMode = SmoothingMode.AntiAlias;
            oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
            oGraphic.DrawImage(oBitmap, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, oBitmap.Width, oBitmap.Height, GraphicsUnit.Pixel);

            oBitmap = bmpNew;

            oGraphic.DrawString("T E S T", font, brush, bmpNew.Width/2, bmpNew.Height/2);


            Encoder myEncoder = Encoder.Compression;
            EncoderParameter myEncoderParameter;
            EncoderParameters myEncoderParameters = new EncoderParameters(1);

            myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionCCITT4);
            myEncoderParameters.Param[0] = myEncoderParameter;
            //watermark does not appear on saved image:
           oBitmap.Save(Server.MapPath("~/PrintQueue/") + "TEST.tiff", GetImageEncoderInfo("image/tiff"), myEncoderParameters);

            //file is more than 10x size as original image:
            //oBitmap.Save(Server.MapPath("~/PrintQueue/") + "TEST.tif", ImageFormat.Tiff);


 protected ImageCodecInfo GetImageEncoderInfo(string mimeType)
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            return null;

Question by:absok
    1 Comment
    LVL 29

    Accepted Solution

    class Watermark helps to add an alpha watermark to the Image file and return a Bitmap Object with watermark text

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    In a previous article published here at Experts Exchange, Signature Image with Transparent Background (, I explained how to cre…
    This article provides the solution to a question ( posed here at Experts Exchange. The asker of the question has many JPG images in many folders, and all of t…
    In this third video of the Xpdf series, we discuss and demonstrate the PDFtoText utility, which converts PDF files into plain text files. Download and install the software.: You may have already downloaded and installed the Xpdf tools while watching…
    Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now