C#水印图像而不重叠

时间:2010-01-18 16:26:17

标签: c#

我要做的是将字符串写入图像而不重叠图像。所以我想给水印图像添​​加水印,将水印(字符串)放在底部而不会实际影响图像或拉伸图像。我已经知道如何将字符串转换为图像,只是在完成水印时遇到问题。

1)将字符串写入右下角的图像
2)不要拉伸原始图像
3)稍微扩展位图以包含水印?

有没有人有一个例子或想法从哪里开始?如果我没有任何意义,我会尝试回答任何问题。

位图示例:

这是我用来获取位图的代码,如何将X大小增加到20?

Bitmap original = (Bitmap)System.Drawing.Image.FromFile(coveted);
Bitmap newImage = new Bitmap(original);

我猜第二部分是如何确定我需要的空间,以便在图像的最右下方写出一个字符串,同时适合整个字符串......

@ROY:这是我做的编辑。

这种方法有效,但一个水印出现在另一个下方。如果我能把它们放在同一条线上那就完美了!

private static Bitmap WatermarkImage2(Bitmap bmpOriginal, String waterMark2)
    {
        using (Graphics gfxOriginal = Graphics.FromImage(bmpOriginal))
        {
            using (Font fntWatermark = new Font("Arial", 24, FontStyle.Regular))
            {
                SizeF szWatermark = gfxOriginal.MeasureString(waterMark2, fntWatermark, int.MaxValue);
                Bitmap bmpWatermarked2 = new Bitmap(bmpOriginal.Width, bmpOriginal.Height + (int)(szWatermark.Height * 2));
                using (Graphics gfxWatermarked = Graphics.FromImage(bmpWatermarked2))
                {
                    gfxWatermarked.Clear(Color.White);
                    gfxWatermarked.DrawImageUnscaled(bmpOriginal, 0, 0);
                    gfxWatermarked.DrawString(waterMark2, fntWatermark, Brushes.Black, 0, (bmpOriginal.Height + szWatermark.Height) - (szWatermark.Height / 2));
                }
                return bmpWatermarked2;
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

这可能对您有用:

    private static Bitmap WatermarkImage(Bitmap bmpOriginal, String waterMarkLeft, String waterMarkRight)
    {
        using (Graphics gfxOriginal = Graphics.FromImage(bmpOriginal))
        {
            using (Font fntWatermark = new Font("Arial", 12, FontStyle.Regular))
            {                    
                SizeF szWatermarkLeft = gfxOriginal.MeasureString(waterMarkLeft, fntWatermark, int.MaxValue);
                SizeF szWatermarkRight = gfxOriginal.MeasureString(waterMarkRight, fntWatermark, int.MaxValue);

                float heightWatermark = szWatermarkLeft.Height > szWatermarkRight.Height ? szWatermarkLeft.Height : szWatermarkRight.Height;

                Bitmap bmpWatermarked = new Bitmap(bmpOriginal.Width, bmpOriginal.Height + (int)(heightWatermark * 2));

                using (Graphics gfxWatermarked = Graphics.FromImage(bmpWatermarked))
                {
                    gfxWatermarked.Clear(Color.White);
                    gfxWatermarked.DrawImageUnscaled(bmpOriginal, 0, 0);
                    gfxWatermarked.DrawString(waterMarkLeft, fntWatermark, Brushes.Black, 0, (bmpOriginal.Height + heightWatermark) - (szWatermarkLeft.Height / 2));
                    gfxWatermarked.DrawString(waterMarkRight, fntWatermark, Brushes.Black, (bmpOriginal.Width - szWatermarkRight.Width), (bmpOriginal.Height + heightWatermark) - (heightWatermark / 2));                        
                }

                return bmpWatermarked;                                        
            }
        }
    }

然后你会这样称呼:

using (Bitmap bmpWatermarked = WatermarkImage((Bitmap)Bitmap.FromFile(@"c:\\test.bmp"), @"Copyright (C) A Corp."))
{
    bmpWatermarked.Save(@"c:\watermarked.bmp");
}

结合您的上述代码:

Bitmap original = (Bitmap)System.Drawing.Image.FromFile(coveted); 
Bitmap newImage = new Bitmap(original); 
using (Bitmap bmpWatermarked = WatermarkImage(newImage, @"Copyleft (C) A Corp.", @"Copyright (C) B Corp."))
{
  bmpWatermarked.Save(@"c:\watermarked.bmp");
}

答案 1 :(得分:1)

听起来你想做的只是让图像比原版更大,并在底部包含你的“水印”,就像图片页脚一样。

您需要做的是创建一个比原始图像大的新Bitmap对象(按水印量)。然后使用Graphics DrawImageUnscaled方法在0,0处绘制原始图像,然后在底部(或右侧或任何位置)绘制水印图像。