Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

winforms - C# - Add watermark to the photo by special way

I need add watermark to the photo by special way. I know how to do it, but I don't know, how to do it the same way as in this article.

Here is method to add watermark. How I can change it to get image with watermark such as in article above?

public static Bitmap AddWatermark(this Bitmap originalImage, Bitmap watermarkImage, WatermarkLocationEnum location)
{
    int offsetWidth;
    int offsetHeight;
    if ((watermarkImage.Width > originalImage.Width) | (watermarkImage.Height > originalImage.Height))
        throw new Exception("The watermark must be smaller than the original image.");
    Bitmap backgroundImage = new Bitmap((Bitmap)originalImage.Clone());
    Bitmap image = new Bitmap(backgroundImage.Width, backgroundImage.Height);
    Graphics graphics = Graphics.FromImage(image);
    offsetWidth = GetOffsetWidth(image.Width, watermarkImage.Width, location);
    offsetHeight = GetOffsetHeight(image.Height, watermarkImage.Height, location);
    watermarkImage.SetResolution(backgroundImage.HorizontalResolution, backgroundImage.VerticalResolution);
    offsetWidth = Math.Max(offsetWidth - 1, 0);
    offsetHeight = Math.Max(offsetHeight - 1, 0);
    graphics.DrawImage(watermarkImage, offsetWidth, offsetHeight);
    for (int i = offsetWidth; i < (offsetWidth + watermarkImage.Width); i++)
    {
        for (int j = offsetHeight; j < (offsetHeight + watermarkImage.Height); j++)
        {
            Color pixel = image.GetPixel(i, j);
            if (pixel.A > 0)
            {
                Color color = Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B);
                Color imagePixelColor = backgroundImage.GetPixel(i, j);
                double alpha = (double)color.A / 255;
                Color newColor = Color.FromArgb(255,
                    (int)((double)imagePixelColor.R * (1.0 - alpha) + alpha * color.R),
                    (int)((double)imagePixelColor.G * (1.0 - alpha) + alpha * color.G),
                    (int)((double)imagePixelColor.B * (1.0 - alpha) + alpha * color.B));
                backgroundImage.SetPixel(i, j, newColor);
            }
        }
    }
    return backgroundImage;
}

//............
Image img = Bitmap.FromFile("DSC00766.JPG");
var wtm = (Bitmap)Bitmap.FromFile("Copyright1.jpg");
((Bitmap)img).AddWatermark(wtm, WatermarkLocationEnum.BottomCenter).Save("new.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

UPDATE

Expected result:

expect result

Current result:

current result

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you create your copyright image so that it is translucent and has a transparent background like this (using Paint.NET):

alt text

you can create a TextureBrush from it and use that to draw the copyright over the original image:

private void button2_Click(object sender, EventArgs e)
{
    using (Image image = Image.FromFile(@"C:UsersPublicPicturesSample PicturesDesert.jpg"))
    using (Image watermarkImage = Image.FromFile(@"C:UsersPublicPicturesSample Pictureswatermark.png"))    
    using (Graphics imageGraphics = Graphics.FromImage(image))
    using (Brush watermarkBrush = new TextureBrush(watermarkImage))
    {
        imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(0, 0), image.Size));
        image.Save(@"C:UsersPublicPicturesSample PicturesDesert_watermark.jpg");
    }
}

which produces this result:

alt text


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...