[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1558
  • Last Modified:

asp.net c# Add watermark using tiff compression

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.Dispose();
            oBitmap = bmpNew;
           

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

            oGraphic.Dispose();
            font.Dispose();
            brush.Dispose();


            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);


            oBitmap.Dispose();
            stream.Dispose();
           
        }

 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;

        }
0
absok
Asked:
absok
1 Solution
 
Kumaraswamy RCommented:
class Watermark helps to add an alpha watermark to the Image file and return a Bitmap Object with watermark text

http://www.dotnetspider.com/resources/23993-Watermark-an-Image-C-net-ASP-net.aspx
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now