What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleAdd a watermark to an image in VB .NET
DescriptionThis example shows how to add a watermark to an image in VB .NET. It makes some parts of the watermark image transparent and others translucent.
Keywordswatermark, image, image processing
Subroutine DrawWatermark copies a watermark image onto another image. First it loops through the watermark's pixels setting the alpha component of each to 128 to make them 50 percent opaque.

It then sets the image's transparent color to the color of the pixel in the upper left corner. This pixel should have the image's background color, as should any other pixels that you want to be transparent in the result.

The program then copies the watermark image onto the result image.

' Copy the watermark image over the result image.
Private Sub DrawWatermark(ByVal watermark_bm As Bitmap, _
    ByVal result_bm As Bitmap, ByVal x As Integer, ByVal y _
    As Integer)
    Const ALPHA As Byte = 128
    ' Set the watermark's pixels' Alpha components.
    Dim clr As Color
    For py As Integer = 0 To watermark_bm.Height - 1
        For px As Integer = 0 To watermark_bm.Width - 1
            clr = watermark_bm.GetPixel(px, py)
            watermark_bm.SetPixel(px, py, _
                Color.FromArgb(ALPHA, clr.R, clr.G, clr.B))
        Next px
    Next py

    ' Set the watermark's transparent color.
    watermark_bm.MakeTransparent(watermark_bm.GetPixel(0, _

    ' Copy onto the result image.
    Dim gr As Graphics = Graphics.FromImage(result_bm)
    gr.DrawImage(watermark_bm, x, y)
End Sub
The example program draws two watermarks, one with the surface of letters transparent and one with the surfaces a neutral color.

You may want to try other alpha values. For example, 64 produces a more subtle result.

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.