C# - 通过特殊方式为照片添加水印 [英] C# - Add watermark to the photo by special way
本文介绍了C# - 通过特殊方式为照片添加水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要通过特殊方式为照片添加水印.我知道怎么做,但我不知道怎么做,就像 这篇 文章.
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);
更新
预期结果:
当前结果:
推荐答案
如果您创建的版权图像是半透明的并且具有像这样的透明背景(使用 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: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");
}
}
产生这个结果:
这篇关于C# - 通过特殊方式为照片添加水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文