C# 自动为图像添加水印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12870137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:45:36  来源:igfitidea点击:

Automatically add watermark to an image

c#winformsalgorithmimage-processing

提问by Riskhan

while searching for a solution to automatically put a watermark to an image in internet, i found a best solution in stackoverflow. Link for the question is C# - Add watermark to the photo by special way. My special thanks to Alex Maslakovand adrift.

在寻找自动为 Internet 中的图像添加水印的解决方案时,我在 stackoverflow 中找到了最佳解决方案。问题的链接是C# - 通过特殊方式为照片添加水印。我特别感谢 Alex Maslakovadrift

I implemented that solution with some modifications, i want to put watermark in center of the image. I modified the solution provided by adriftas follows

我通过一些修改实现了该解决方案,我想将水印放在图像的中心。我修改了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");
        }

    }

but watermark is not properly placed in the center of image (see below image).

但是水印没有正确放置在图像的中心(见下图)。

enter image description here

在此处输入图片说明

please correct me...

请纠正我...

thanks

谢谢

采纳答案by Riskhan

Finally i find the solution to my question...

最后我找到了我的问题的解决方案......

The corrected code answer is following

更正后的代码答案如下

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

    }

my thanks to Furqan Safdar and Abdias Software The link Problem in tiling image starting at different height using TextureBrush in C#also helped me to solve this problem

感谢 Furqan Safdar 和 Abdias Software在 C# 中使用 TextureBrush 在不同高度开始平铺图像的链接问题也帮助我解决了这个问题

and thanks all

并感谢大家

finally result

最终结果

enter image description here

在此处输入图片说明

回答by Furqan Safdar

Try this code for center alignment:

尝试使用此代码进行中心对齐:

int x = (image.Width / 2 - watermarkImage.Width / 2);
int y = (image.Height / 2 - watermarkImage.Height / 2);

回答by Furqan Safdar

Your original formula is fine btw.,

顺便说一句,您的原始公式很好,

are you making sure the resolution of the two are the same? Set DPI resolution of your watermark image equal to image:

你确定两者的分辨率是一样的吗?将水印图像的 DPI 分辨率设置为等于图像:

watermarkImage.SetResolution(imageGraphics.DpiX, imageGraphics.DpiY);

(from the top of my head, but i think the property names are correct).

(从我的头顶,但我认为属性名称是正确的)。

UPDATE:

更新:

Change from Image to Bitmap in order to use SetResolution()- See if this change works for you - I've changed the way the watermark is drawn onto the image:

将图像更改为位图以使用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");
    }
}

回答by Er?in Dedeo?lu

Working sample:

工作样本:

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

Usage:

用法:

Bitmap watermark = new Bitmap(@"c:/watermark.png");
Bitmap bitmap = new Bitmap(@"c:/image.png");
bitmap = WatermarkImage(bitmap, watermark);
bitmap.Save(@"C:/watermarkedImage.png");