如何根据图像大小设置图像中的水印文本的字体大小和位置?

时间:2016-09-25 07:30:53

标签: c# image watermark

如何根据图像尺寸设置图像中的水印文字的字体大小和位置?

我正在寻找一种根据图像尺寸改变字体大小的解决方案。

也是透明水印。

我的职能:

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例子:

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

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

感谢

0 个答案:

没有答案
相关问题