Make translucent text suitable for image watermarking in in C#

This example uses the following code to draw translucent text on top of an image.

// Draw translucent text.
private void Form1_Load(object sender, EventArgs e)
{
Bitmap bm = (Bitmap)picSrc.Image.Clone();
using (Graphics gr = Graphics.FromImage(bm))
{
using (StringFormat string_format = new StringFormat())
{
string_format.Alignment = StringAlignment.Center;

int dy = (int)(gr.MeasureString("X", this.Font).Height * 1.2);
int x = bm.Width / 2;
int y = 60;

for (int opacity = 31; opacity <= 255; opacity += 32)
{
string txt = "DRAWN WITH OPACITY " + opacity.ToString();
using (Brush brush = new SolidBrush(Color.FromArgb(opacity, 0, 0, 0)))
{
gr.DrawString(txt, this.Font, brush, x, y, string_format);
}
using (Brush brush = new SolidBrush(Color.FromArgb(opacity, 255, 255, 255)))
{
gr.DrawString(txt, this.Font, brush, x - 1, y - 1, string_format);
}
y += dy;
}
}

picResult.Image = bm;
}
}

To make translucent text, the program draws with two brushes. To make the brushes translucent, the program creates them using colors with alpha (opacity) values less than 255. The program draws the text using a translucent black brush and then draws it again shifted one pixel left and up with a translucent white brush. The final picture is slightly darkened along the lower and right edges of the text and lightened on the text's body.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.