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
305 views
in Technique[技术] by (71.8m points)

c# - Automatically add watermark to an image

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 Maslakov and adrift.

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

   private void button1_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))
        {
            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:UsersPublicPicturesSample PicturesDesert_watermark.jpg");
        }

    }

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

enter image description here

please correct me...

thanks

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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:UsersPublicPicturesSample PicturesDesert.jpg"))
        using (Image watermarkImage = Image.FromFile(@"C:UsersPublicPicturesSample Pictureswatermark.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:UsersPublicPicturesSample PicturesDesert_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

and thanks all

finally result

enter image description here


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

...