Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:\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

like image 578
Riskhan Avatar asked Oct 13 '12 05:10

Riskhan


People also ask

How do I watermark my photos quickly?

Another easy way to watermark a photo is to use an online tool like PicMarkr. Upload up to five photos, or pull them from Flickr or Facebook, then pick from three watermarking options (text, image, or tiled).

How do I add a watermark to a photo?

With the picture loaded, tap Tools at the bottom of the screen then 'Double Exposure'. After that, tap the “Add Image” icon at the bottom of the screen, find the image you want to use as a watermark, then resize and position it, then tap the tick icon in the bottom right corner.

How do I add a watermark to a batch of photos?

Import all photos you want to add a watermark to BatchPhoto. Head to Edit Photos>Add Filters>Watermark Text. Then input the watermark text, choose placement and font style, click OK to save the settings.


1 Answers

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

and thanks all

finally result

enter image description here

like image 186
Riskhan Avatar answered Sep 28 '22 04:09

Riskhan