C#中 - 添加水印的照片通过特殊的方式水印、特殊、方式、照片

2023-09-11 02:03:05 作者:摆摊卖爱情

我需要添加水印的照片通过特殊的方式。我知道该怎么做,但我不知道如何以同样的方式做到这一点在文章http://www.photoshopessentials.com/photo-effects/copyright/

I need add watermark to the photo by special way. I know how to do it, but I dunno how to do it the same way as in article http://www.photoshopessentials.com/photo-effects/copyright/

下面是方法添加水印。我怎样才能改变它来获得,如在文章上面的图片有水印?

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

更新

1 attach-预期结果 - 2 attach-当前结果

1 attach- expect result 2 attach- current result

推荐答案

如果您创建了您的版权图像,它是半透明的,有一个透明的背景是这样的(使用Paint.NET):

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

您可以创建一个的TextureBrush 从,并用它来绘制的版权在原始图像:

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:\Users\Public\Pictures\Sample Pictures\Desert.jpg"))
    using (Image watermarkImage = Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\watermark.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:\Users\Public\Pictures\Sample Pictures\Desert_watermark.jpg");
    }
}

这会产生这样的结果:

which produces this result: