7

I have a scenario where the Watermark text size should be assigned automatically as per the Image size. I am new to C# drawing features. Please help me get some workaround for this.

Current Logic to apply watermark text with fixed size on the image

protected byte[] WatermarkImage(string PhysicalPath, string Watermarktext)
{

        byte[] imageBytes = null;
        if (File.Exists(PhysicalPath))
        {
            // This is the Name which will appear as a watermark on image.
            string watermark = Watermarktext;

            Image image = Image.FromFile(PhysicalPath);

            Graphics graphic;
            if (image.PixelFormat != PixelFormat.Indexed && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format4bppIndexed && image.PixelFormat != PixelFormat.Format1bppIndexed)
            {

                graphic = Graphics.FromImage(image);
            }
            else
            {

                Bitmap indexedImage = new Bitmap(image);
                graphic = Graphics.FromImage(indexedImage);

                // Draw the contents of the original bitmap onto the new bitmap.
                graphic.DrawImage(image, 0, 0, image.Width, image.Height);
                image = indexedImage;
            }
            graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;
            //This is the font for your watermark
            int size = 30; int opacity = 100;
            Font myFont = new Font("Arial", size, FontStyle.Bold, GraphicsUnit.Pixel);
            SolidBrush brush = new SolidBrush(Color.FromArgb(opacity, Color.Beige));

            //This gets the size of the graphic

            SizeF textSize = graphic.MeasureString(watermark, myFont);
            graphic.TranslateTransform(image.Width / 2, image.Height / 2);
            var angle = -45f;
            graphic.RotateTransform(angle);
            var x = -(textSize.Width / 2);
            var y = -(textSize.Height / 2);
            // Code for writing text on the image and showing its postion on images.
            //graphic.RotateTransform(45);
            PointF pointF = new PointF(x, y);

            graphic.DrawString(watermark, myFont, brush, pointF);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                image.Save(memoryStream, GetImageFormat(PhysicalPath));
                imageBytes = memoryStream.ToArray();
            }
        }
        return imageBytes;
}
3
  • I don't know how to calculate what font size you need ahead of time (you might want to pre-calculate a bunch of scenarios and reference them) but you can measure the string to see if it will fit. Commented Oct 5, 2018 at 12:48
  • what is your problem with this code ? Commented Oct 5, 2018 at 12:49
  • @Siavash Ghanbari: I have no problem with my code but just the watermark text appears very tiny when the image size is bigger. say like ("3840*2160). For that I need some way to define the watermark text based on the image size. So, the watermark text can be visible irrespective of the image size. Commented Oct 5, 2018 at 12:53

1 Answer 1

6

you just need to change your size value, 30 is a fixed and change it to below code:

int countOfChar = Watermarktext.Length;
int size = (image.Width + image.Height / 2) / countOfChar;

I tested your code with a little changes, and that worked for all image sizes and dimensions.

Sign up to request clarification or add additional context in comments.

2 Comments

I have done little changes to your code and it works fine for all size images. int size = ((image.Width + image.Height) / 2) / countOfChar;
Don't know your but my watermark text on my images is still not dynamic... it goes out of the image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.