0

How can the font size and the position of the watermark text in an image based on image size set?

I'm looking for a solution to alter the font size according to the image size.

also Transparent watermark.

my function:

public void fn_TextWaterMark(string text,ImageFormat fmt)
{
    string sourceImage = "c:\\a.jpg;
    string targetImage = "c:\\a-watermark.jpg;

  try
  {
    //open source image as stream and create a memorystream for output
    FileStream source = new FileStream(sourceImage, FileMode.Open);
    Stream output = new MemoryStream();
    Image img = Image.FromStream(source);

    //choose font for text
    Font font = new Font("Times New Roman", 20, FontStyle.Bold, GraphicsUnit.Pixel);

    //choose color and transparency
    Color color = Color.FromArgb(255, 255, 255, 255);

    //location of the watermark text in the parent image
    default//Point pt = new Point(100, 100);
    Point pt = new Point(img.Width*31/100, img.Height*46/100);
    SolidBrush brush = new SolidBrush(color);

    //draw text on image
    Graphics graphics = Graphics.FromImage(img);
    graphics.DrawString(text, font, brush, pt);
    graphics.Dispose();

    //update image memorystream
    img.Save(output, fmt);
    Image imgFinal = Image.FromStream(output);

    //write modified image to file
    Bitmap bmp = new System.Drawing.Bitmap(img.Width,img.Height, img.PixelFormat);
    Graphics graphics2 = Graphics.FromImage(bmp);
    graphics2.DrawImage(imgFinal, new Point(0,0));
    bmp.Save(targetImage, fmt);

    imgFinal.Dispose();
    img.Dispose();
   }

  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }          
}

pic examp:

https://i.stack.imgur.com/ZShUI.jpg

https://i.stack.imgur.com/GUfbM.jpg

thanks

Sunil Kumar
  • 3,142
  • 1
  • 19
  • 33
cyrus2500
  • 428
  • 4
  • 15
  • Could you share the image I mean how it looks now? – AT-2017 Sep 25 '16 at 07:42
  • add pic for u @AT-2016 – cyrus2500 Sep 25 '16 at 08:14
  • An image already has a reference number that say how big it should be when displayed on, say, a monitor or printer. Exposed through the Image.HorizontalResolution and VerticalResolution properties. Any text you add to the image should observe this value so the text displays/prints at the intended size as well. That is completely automatic, **except** when you pass GraphicsUnit.Pixel to the Font constructor. Then you say you want to ignore the resolution and always get 20 pixels. Well, don't do that. You actually do like the default, GraphicsUnit.Point. – Hans Passant Sep 25 '16 at 11:58

0 Answers0