开发者

C# Watermark an image without overlapping

What i'm trying to do is write a string to an image without overlapping th开发者_开发技巧e image. So i want to watermark an image putting the watermark (string) at the bottom without actually affecting the image or stretching the image out. I already know how to turn a string into an image, just having problems accomplishing the watermark.

1) Write string to Image on bottom right hand side

2) don't stretch the original image

3) extend the bitmap a little bit to include watermark?

Does anybody have an example or an idea where to start? If i don't make any sense i'll try to answer any questions.

Bitmap example:

here is the code I use to get the bitmap, how do I increase just the size by X like 20?

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

And I guess the 2nd part is how do i determine the space that I need in order to write out a string to the very bottom right of the image, while fitting in the entire string...

@ROY: This is the edit that i did.

This sorta works but the one watermark appears below the other. if i could get them on the same line it would be perfect!

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;
            }
        }
    }


This may work for you:

    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;                                        
            }
        }
    }

Then you would call it like this:

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

Combined with your code above:

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");
}


It sounds like what you want to do is simply make the image larger than the original, and include your "watermark" at the bottom like an image footer.

What you need to do is to create a new Bitmap object that is larger than the original image (by the amount of the watermark). Then use the Graphics DrawImageUnscaled method to draw the original image at 0,0 and then draw your Watermark Image at the bottom (or right, or wherever).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜