user1711092
user1711092

Reputation:

WaterMarking on Image in C#?

I want to put water mark on center of image diagonally.My code doing well but water mark not appear in center of every image.I think there is problem in postioning of watermark text where i hard code values.How can I write generic formula for postioning of watermark text.

    private MemoryStream ApplyWaterMark(MemoryStream stream)
        {

            System.Drawing.Image Im = System.Drawing.Image.FromStream(stream);                // 
            Graphics g = Graphics.FromImage(Im);

            // Create a solid brush to write the watermark text on the image
            Brush myBrush = new SolidBrush(System.Drawing.Color.FromArgb(25,
            System.Drawing.Color.LightSteelBlue));

            var f = new System.Drawing.Font(FontFamily.GenericSerif, 30);
            // Calculate the size of the text
            SizeF sz = g.MeasureString("TEST WATER MARK", f);

            int X, Y;
            X = ((int)(Im.Width - sz.Width) / 2)-1162;
            Y = ((int)(Im.Height + sz.Height) / 2)-127;



            g.RotateTransform(-45f);
            g.DrawString("TEST WATER MARK", f, myBrush,new System.Drawing.Point(X, Y));
            g.RotateTransform(45f);

            Im.Save(OutPutStream, ImageFormat.Png);//save image with dynamic watermark

            return OutPutStream;
        }

Upvotes: 1

Views: 4801

Answers (3)

Rao
Rao

Reputation: 41

Water mark at the bottom with multiple colors

public void AddWatermark(string watermarkText, string image, string TColor) 
{       

    System.Drawing.Image bitmap = (System.Drawing.Image)Bitmap.FromFile(Proot + image);
    Font font = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Pixel);

    RectangleF rectf = new RectangleF(70, 90, 90, 50);

    Color color = Color.FromArgb(255, 255, 0, 0);
    try
    {
        if (TColor.ToUpper() == "RED")
        {
            color = Color.FromArgb(255, 255, 0, 0);
        }
        else if (TColor.ToUpper() == "WHITE")
        {
            color = Color.FromArgb(255, 255, 255, 255);
        }
        else if (TColor.ToUpper() == "BLACK")
        {
            color = Color.FromArgb(155, 0, 0, 0);
        }
        else if (TColor.ToUpper() == "GREEN")
        {
            color = Color.FromArgb(255, 0, 255, 0);
        }
    }
    catch { }


    //     WHITE (255, 255, 255, 255)
    //     RED   (255, 255, 000, 000)
    //     GREEN (255, 000, 255, 000)
    //     BLUE  (255, 000, 000, 255) 
    //     BLACK (150, 000, 000, 000)
    //     PURPLE(255, 125, 000, 255)
    //     GREY  (255, 128, 128, 128) 
    //     YELLOW(255, 255, 255, 000) 
    //     ORANGE(255, 255, 125, 000)

 //   Point atpoint = new Point(bitmap.Width / 2, bitmap.Height / 2);
    Point atpoint = new Point(bitmap.Width / 2, bitmap.Height - 10);

    SolidBrush brush = new SolidBrush(color);
    Graphics graphics = Graphics.FromImage(bitmap);

    StringFormat sf = new StringFormat();
    sf.Alignment = StringAlignment.Center;
    sf.LineAlignment = StringAlignment.Center;

    graphics.DrawString(watermarkText, font, brush, atpoint, sf);
    graphics.Dispose();
    MemoryStream m = new MemoryStream();
    bitmap.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);

 //   Response.WriteFile("images/DefaultLogo.png", true);
    m.WriteTo(Response.OutputStream);

    m.Dispose();
    base.Dispose();
}

Upvotes: 0

user1640277
user1640277

Reputation:

You can set the watermark diagonally by this way

     Bitmap newBitmap = new Bitmap(bitmap);
     Graphics g = Graphics.FromImage(newBitmap);

     // Trigonometry: Tangent = Opposite / Adjacent
     double tangent = (double)newBitmap.Height / 
                      (double)newBitmap.Width;

     // convert arctangent to degrees
     double angle = Math.Atan(tangent) * (180/Math.PI);

     // a^2 = b^2 + c^2 ; a = sqrt(b^2 + c^2)
     double halfHypotenuse =(Math.Sqrt((newBitmap.Height 
                            * newBitmap.Height) +
                            (newBitmap.Width * 
                            newBitmap.Width))) / 2;

     // Horizontally and vertically aligned the string
     // This makes the placement Point the physical 
     // center of the string instead of top-left.
     StringFormat stringFormat = new StringFormat();
     stringFormat.Alignment = StringAlignment.Center;
     stringFormat.LineAlignment=StringAlignment.Center;

     g.RotateTransform((float)angle);            
     g.DrawString(waterMarkText, font, new SolidBrush(color),
                  new Point((int)halfHypotenuse, 0), 
                  stringFormat);

Upvotes: 6

Furqan Safdar
Furqan Safdar

Reputation: 16708

Try computing the coordinates (X, Y) this way for center of the image:

int X, Y;
X = (int)(Im.Width / 2 - sz.Width / 2);
Y = (int)(Im.Height / 2 - sz.Height / 2);

Upvotes: 0

Related Questions