在搜索自动将水印添加到互联网上的图像的解决方案时,我在stackoverflow中找到了最佳解决方案。该问题的链接是C# - Add watermark to the photo by special way。我要特别感谢 Alex Maslakov 和 adrift 。
我通过一些修改实现了该解决方案,我想将水印放在图像的中心。我修改了 adrift 提供的解决方案,如下所示
private void button1_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))
{
int x = (image.Width - watermarkImage.Width)/2;
int y = (image.Height - watermarkImage.Height)/2;
imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), watermarkImage.Size));
image.Save(@"C:\Users\Public\Pictures\Sample Pictures\Desert_watermark.jpg");
}
}
但水印未正确放置在图像中心(见下图)。
请纠正我......
感谢
答案 0 :(得分:20)
最后我找到了我的问题的解决方案......
更正后的代码答案如下
private void button1_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 (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
{
int x = (image.Width / 2 - watermarkImage.Width / 2);
int y = (image.Height / 2 - watermarkImage.Height / 2);
watermarkBrush.TranslateTransform(x, y);
imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width+1, watermarkImage.Height)));
image.Save(@"C:\Users\Public\Pictures\Sample Pictures\Desert_watermark.jpg");
}
}
我要感谢Furqan Safdar和Abdias Software 链接Problem in tiling image starting at different height using TextureBrush in C#也帮助我解决了这个问题
并感谢所有
最终结果
答案 1 :(得分:7)
您原来的公式很好,
你确定两者的分辨率是一样的吗?将水印图像的DPI分辨率设置为等于图像:watermarkImage.SetResolution(imageGraphics.DpiX, imageGraphics.DpiY);
(从我的头脑,但我认为属性名称是正确的。)
更新:
从图像更改为位图以便使用SetResolution()
- 查看此更改是否适合您 - 我已更改水印在图像上的绘制方式:
private void button1_Click(object sender, EventArgs e) {
using (Bitmap image = Bitmap.FromFile("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"))
using (Bitmap watermarkImage = Bitmap.FromFile("C:\Users\Public\Pictures\Sample Pictures\watermark.png"))
using (Graphics imageGraphics = Graphics.FromImage(image))
{
watermarkImage.SetResolution(imageGraphics.DpiX, imageGraphics.DpiY);
int x = ((image.Width - watermarkImage.Width) / 2);
int y = ((image.Height - watermarkImage.Height) / 2);
imageGraphics.DrawImage(watermarkImage, x, y, watermarkImage.Width, watermarkImage.Height);
image.Save("C:\Users\Public\Pictures\Sample Pictures\Desert_watermark.jpg");
}
}
答案 2 :(得分:2)
尝试使用此代码进行居中对齐:
int x = (image.Width / 2 - watermarkImage.Width / 2);
int y = (image.Height / 2 - watermarkImage.Height / 2);
答案 3 :(得分:0)
工作样本:
public static Bitmap WatermarkImage(Bitmap image, Bitmap watermark)
{
using (Graphics imageGraphics = Graphics.FromImage(image))
{
watermark.SetResolution(imageGraphics.DpiX, imageGraphics.DpiY);
int x = (image.Width - watermark.Width) / 2;
int y = (image.Height - watermark.Height) / 2;
imageGraphics.DrawImage(watermark, x, y, watermark.Width, watermark.Height);
}
return image;
}
<强>用法:强>
Bitmap watermark = new Bitmap(@"c:/watermark.png");
Bitmap bitmap = new Bitmap(@"c:/image.png");
bitmap = WatermarkImage(bitmap, watermark);
bitmap.Save(@"C:/watermarkedImage.png");