Add Watermark Text to TIFF Image using C# and VB.Net

Last Reply 9 months ago By dharmendr

Posted 9 months ago

Hi,

I want to display watermark on the tiff image using c#

Can anyone help me?

    protected void Upload(object sender, EventArgs e)
    {
        string watermarkText = "copy rights";
        //Get the file name.
        string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) + ".png";

        //Read the File into a Bitmap.
        using (Bitmap bmp = new Bitmap(FileUpload1.PostedFile.InputStream, false))
        {
            using (Graphics grp = Graphics.FromImage(bmp))
            {
                //Set the Color of the Watermark text.
                Brush brush = new SolidBrush(Color.Red);

                //Set the Font and its size.
                Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);

                //Determine the size of the Watermark text.
                SizeF textSize = new SizeF();
                textSize = grp.MeasureString(watermarkText, font);

                //Position the text and draw it on the image.
                Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 10)));
                grp.DrawString(watermarkText, font, brush, position);

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    //Save the Watermarked image to the MemoryStream.
                    bmp.Save(memoryStream, ImageFormat.Png);
                    memoryStream.Position = 0;

                    //Start file download.
                    Response.Clear();
                    Response.ContentType = "image/png";
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);

                    //Write the MemoryStream to the Response.
                    memoryStream.WriteTo(Response.OutputStream);
                    Response.Flush();
                    Response.Close();
                    Response.End();
                }
            }
        }
    }

 

Posted 9 months ago

Hi Vinoth548,

Check this example. Now please take its reference and correct your code.

Namespaces

C#

using System.Drawing;
using System.IO;

VB.Net

Imports System.Drawing
Imports System.IO

Code

C#

protected void Upload(object sender, EventArgs e)
{
    string fileName = @"C:\Users\dharmendra\Desktop\Jellyfish.tiff";
    string watermarkText = "copy rights";
    Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
    System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
    using (Graphics grp = Graphics.FromImage(image))
    {
        Brush brush = new SolidBrush(Color.Red);
        Font font = new Font("Arial", 30.0f, FontStyle.Bold, GraphicsUnit.Pixel);
        SizeF textSize = grp.MeasureString(watermarkText, font);
        Point position = new Point((image.Width - ((int)textSize.Width + 10)), (image.Height - ((int)textSize.Height + 10)));
        grp.DrawString(watermarkText, font, brush, position);
        stream.Dispose();
        File.Delete(fileName);
        image.Save(fileName);
    }
}

VB.Net

Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
    Dim fileName As String = "C:\Users\dharmendra\Desktop\tiff\Jellyfish.tiff"
    Dim watermarkText As String = "copy rights"
    Dim stream As Stream = New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
    Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(stream)
    Using grp As Graphics = Graphics.FromImage(image)
        Dim brush As Brush = New SolidBrush(Color.Red)
        Dim font As Font = New Font("Arial", 30.0F, FontStyle.Bold, GraphicsUnit.Pixel)
        Dim textSize As SizeF = grp.MeasureString(watermarkText, font)
        Dim position As Point = New Point((image.Width - (CInt(textSize.Width) + 10)), (image.Height - (CInt(textSize.Height) + 10)))
        grp.DrawString(watermarkText, font, brush, position)
        stream.Dispose()
        File.Delete(fileName)
        image.Save(fileName)
    End Using
End Sub

Screenshot

Before Adding Watermark

After Adding Watermark