Saturday, May 10, 2008

How to WaterMark an Image file

Nowadays, we place a lot of images on Internet which can be easily copied and used by others. One way is to place a watermark on the image so that even if others copy your images, you copyright or whatever note you have will still be in the image.

This is how you can do that using

Public Sub addWaterMark(ByVal frmFileName As String, ByVal toFileName As String, ByVal pStr As String)
Dim Img As Image
Dim g As Graphics
Dim brsh As Brush

Img = Image.FromFile(frmFileName)
g = Graphics.FromImage(Img)
Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
fontFamily, _
60, _
FontStyle.Regular, _
brsh = Brushes.Gray
g.DrawString(pStr, font, Brushes.Gray, 300, 50)
g.DrawImage(Img, 0.0F, 0.0F)
Console.WriteLine("Watermark done")

Catch ex As Exception
Throw New Exception("[addWaterMark01]" & ex.Message)
End Try
End Sub

As shown above,
  1. First load the image file into an image object
  2. Create a Graphics object from the image object.
  3. Then write the message you want, using the font, color and size needed.
  4. Draw the Graphics back into the image.
  5. Then save the image to the new file (so you still have your original file.
If you want you can change it further to process a folder instead of a file so that you can perform the watermarking in batch mode prior to uploading into internet.

1 comment:

Strovek said...

Sample of the watermark can be found in my other blog.