Watermarking an Image in the RasterMaster™ SDK for the .NET Platform

Snowbound Software’s RasterMaster Viewing and Conversion SDK product for .NET fully integrates with Microsoft’s Visual Studio for .NET tools providing managed tools capabilities. One of these capabilities is that it allows you to watermark an image. This can provide extra security for your documents.

The code sample below explains how to watermark an image in Microsoft Visual Basic .NET:

Imports System

Imports System.Collections

Imports System.ComponentModel

Imports System.Windows.Forms

Imports System.Data

Imports SbdNetLib

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

Private simage, wmimage As SbdNetLib.Snowbnd

Private imgName, watermarkName As String

Private iImgLoaded, iWmLoaded As Integer

Private statusImg, statusWM As Integer

Private sdibHeader As SbdNetLib.CDIB_HEADER

Private iImgWidth, iImgHeight As Integer

Private iWmWidth, iWmHeight As Integer

Private iWmLength, iImgLength As Integer

Private iWmColor, iImgColor As Integer

Private arrImageData As Byte()

Private arrWMarkData As Byte()

Public Const RThredH = 255, GThredH = 255, BThredH = 255

Public Const RThredL = 186,GThredL = 186, BThredL = 186

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents menuMain As System.Windows.Forms.MainMenu

Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem

Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem

Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem

Friend WithEvents menuOpenImage As System.Windows.Forms.MenuItem

Friend WithEvents menuOpenWatermark As System.Windows.Forms.MenuItem

Friend WithEvents menuSave As System.Windows.Forms.MenuItem

Friend WithEvents menuExit As System.Windows.Forms.MenuItem

Friend WithEvents menuRemove As System.Windows.Forms.MenuItem

Friend WithEvents menuMerge As System.Windows.Forms.MenuItem

Friend WithEvents menuXOR As System.Windows.Forms.MenuItem

Friend WithEvents menuColor As System.Windows.Forms.MenuItem

Friend WithEvents menuMergeFunc As System.Windows.Forms.MenuItem

Friend WithEvents ofDialog As System.Windows.Forms.OpenFileDialog

Friend WithEvents PanWatermark As System.Windows.Forms.Panel

Friend WithEvents PanImage As System.Windows.Forms.Panel

Friend WithEvents sfDialog As System.Windows.Forms.SaveFileDialog

Private Sub InitializeComponent()

Me.menuMain = New System.Windows.Forms.MainMenu

Me.MenuItem1 = New System.Windows.Forms.MenuItem

Me.menuOpenImage = New System.Windows.Forms.MenuItem

Me.menuOpenWatermark = New System.Windows.Forms.MenuItem

Me.menuSave = New System.Windows.Forms.MenuItem

Me.menuExit = New System.Windows.Forms.MenuItem

Me.MenuItem2 = New System.Windows.Forms.MenuItem

Me.menuRemove = New System.Windows.Forms.MenuItem

Me.menuMerge = New System.Windows.Forms.MenuItem

Me.menuXOR = New System.Windows.Forms.MenuItem

Me.menuColor = New System.Windows.Forms.MenuItem

Me.menuMergeFunc = New System.Windows.Forms.MenuItem

Me.MenuItem3 = New System.Windows.Forms.MenuItem

Me.ofDialog = New System.Windows.Forms.OpenFileDialog

Me.sfDialog = New System.Windows.Forms.SaveFileDialog

Me.PanWatermark = New System.Windows.Forms.Panel

Me.PanImage = New System.Windows.Forms.Panel

Me.SuspendLayout()

'

'menuMain

'

Me.menuMain.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem2, Me.MenuItem3})

'

'MenuItem1

'

Me.MenuItem1.Index = 0

Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.menuOpenImage, Me.menuOpenWatermark, Me.menuSave, Me.menuExit})

Me.MenuItem1.Text = "&File"

'

'menuOpenImage

'

Me.menuOpenImage.Index = 0

Me.menuOpenImage.Text = "Open &Image"

'

'menuOpenWatermark

'

Me.menuOpenWatermark.Index = 1

Me.menuOpenWatermark.Text = "Open &Watermark"

'

'menuSave

'

Me.menuSave.Index = 2

Me.menuSave.Text = "&Save Image"

'

'menuExit

'

Me.menuExit.Index = 3

Me.menuExit.Text = "&Exit"

'

'MenuItem2

'

Me.MenuItem2.Index = 1

Me.MenuItem2.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.menuRemove, Me.menuMerge, Me.menuXOR, Me.menuColor, Me.menuMergeFunc})

Me.MenuItem2.Text = "&Watermark"

'

'menuRemove

'

Me.menuRemove.Index = 0

Me.menuRemove.Text = "&Remove WaterMark"

'

'menuMerge

'

Me.menuMerge.Index = 1

Me.menuMerge.Text = "&Merge WaterMark"

'

'menuXOR

'

Me.menuXOR.Index = 2

Me.menuXOR.Text = "&XOR WaterMark"

'

'menuColor

'

Me.menuColor.Index = 3

Me.menuColor.Text = "Merge With &Given Color"

'

'menuMergeFunc

'

Me.menuMergeFunc.Index = 4

Me.menuMergeFunc.Text = "Merge &function"

'

'MenuItem3

'

Me.MenuItem3.Index = 2

Me.MenuItem3.Text = "&Options"

Me.MenuItem3.Visible = False

'

'PanWatermark

'

Me.PanWatermark.Location = New System.Drawing.Point(16, 16)

Me.PanWatermark.Name = "PanWatermark"

Me.PanWatermark.Size = New System.Drawing.Size(168, 120)

Me.PanWatermark.TabIndex = 0

'

'PanImage

'

Me.PanImage.Location = New System.Drawing.Point(16, 144)

Me.PanImage.Name = "PanImage"

Me.PanImage.Size = New System.Drawing.Size(720, 368)

Me.PanImage.TabIndex = 1

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(752, 557)

Me.Controls.Add(Me.PanImage)

Me.Controls.Add(Me.PanWatermark)

Me.Menu = Me.menuMain

Me.Name = "Form1"

Me.Text = "Watermark sample"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

simage = New Snowbnd

wmimage = New Snowbnd

End Sub

Private Sub PanWatermark_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PanWatermark.Paint

If (iWmLoaded = 1) Then wmimage.CIMG_display_bitmap(e.Graphics, 0, 0, PanWatermark.Width, PanWatermark.Height)

End Sub

Private Sub PanImage_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PanImage.Paint

If (iImgLoaded = 1) Then simage.CIMG_display_bitmap(e.Graphics, 0, 0, PanImage.Width, PanImage.Height)

End Sub

Private Sub menuOpenImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuOpenImage.Click

ofDialog.Filter = "Image files (*.bmp)|*.tif|All files (*.*)|*.*"

ofDialog.FilterIndex = 2

ofDialog.RestoreDirectory = True

If (ofDialog.ShowDialog() = DialogResult.OK) Then

imgName = ofDialog.FileName

simage.CIMGLOW_set_alias(4)

statusImg = simage.CIMG_decompress_bitmap(imgName)

If (statusImg >= 0) Then

iImgLoaded = 1

sdibHeader = New CDIB_HEADER

statusImg = simage.CIMG_bitmap_info(sdibHeader)

If (sdibHeader.biHeight > 0) Then PanImage.Width = PanImage.Height * sdibHeader.biWidth / sdibHeader.biHeight

iImgWidth = sdibHeader.biWidth

iImgHeight = sdibHeader.biHeight

End If

PanImage.Refresh()

End If

End Sub

Private Sub menuOpenWatermark_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuOpenWatermark.Click

ofDialog.Filter = "Image files (*.bmp)|*.tif|All files (*.*)|*.*"

ofDialog.FilterIndex = 2

ofDialog.RestoreDirectory = True

If (ofDialog.ShowDialog() = DialogResult.OK) Then

watermarkName = ofDialog.FileName

wmimage.CIMGLOW_set_alias(4)

statusWM = wmimage.CIMG_decompress_bitmap(watermarkName)

If (statusWM >= 0) Then

iWmLoaded = 1

sdibHeader = New CDIB_HEADER

statusWM = wmimage.CIMG_bitmap_info(sdibHeader)

If (sdibHeader.biHeight > 0) Then PanWatermark.Width = PanWatermark.Height * sdibHeader.biWidth / sdibHeader.biHeight

iWmWidth = sdibHeader.biWidth

iWmHeight = sdibHeader.biHeight

iWmColor = sdibHeader.biBitCount

End If

PanWatermark.Refresh()

End If

End Sub

Private Sub menuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuSave.Click

End Sub

Private Sub menuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuExit.Click

Me.Dispose()

End Sub

Private Sub menuRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuRemove.Click

Dim r, g, b As Byte

If (iWmColor < 24) Then statusWM = wmimage.CIMG_promote_24()

If (iWmColor < 24) Then statusImg = simage.CIMG_promote_24()

iWmLength = iWmWidth * 3

iImgLength = iImgWidth * 3

Dim arrImageData(iImgLength) As Byte

Dim arrWMarkData(iWmLength) As Byte

For i As Integer = 1 To iWmHeight - 1

statusWM = wmimage.CIMGLOW_get_raster(arrWMarkData, i, iWmLength)

statusImg = simage.CIMGLOW_get_raster(arrImageData, i, iImgLength)

Dim iXStart As Integer = 0

For j As Integer = iXStart To iWmWidth - 1

r = arrWMarkData(3 * j)

g = arrWMarkData(3 * j + 1)

b = arrWMarkData(3 * j + 2)

If ((r >= RThredL) And (g >= GThredL) And (b >= BThredL)) Then

arrImageData(3 * j) = arrImageData(3 * j) Xor 255

arrImageData(3 * j + 1) = arrImageData(3 * j + 1) Xor 255

arrImageData(3 * j + 2) = arrImageData(3 * j + 2) Xor 255

End If

Next j

statusImg = simage.CIMGLOW_put_raster(arrImageData, i, iImgLength)

Next i

PanImage.Refresh()

End Sub

Private Sub menuMerge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuMerge.Click

Dim r, g, b As Byte

If (iWmColor < 24) Then statusWM = wmimage.CIMG_promote_24()

If (iWmColor < 24) Then statusImg = simage.CIMG_promote_24()

iWmLength = iWmWidth * 3

iImgLength = iImgWidth * 3

Dim arrImageData(iImgLength) As Byte

Dim arrWMarkData(iWmLength) As Byte

For i As Integer = 1 To iWmHeight - 1

statusWM = wmimage.CIMGLOW_get_raster(arrWMarkData, i, iWmLength)

statusImg = simage.CIMGLOW_get_raster(arrImageData, i, iImgLength)

Dim iXStart As Integer = 0

For j As Integer = iXStart To iWmWidth - 1

r = arrWMarkData(3 * j)

g = arrWMarkData(3 * j + 1)

b = arrWMarkData(3 * j + 2)

If (((r < RThredL) Or (r > RThredH)) And ((g < GThredL) Or (g > GThredH)) And ((b < BThredL) Or (b > BThredH))) Then

arrImageData(3 * j) = arrWMarkData(3 * j)

arrImageData(3 * j + 1) = arrWMarkData(3 * j + 1)

arrImageData(3 * j + 2) = arrWMarkData(3 * j + 2)

End If

Next j

statusImg = simage.CIMGLOW_put_raster(arrImageData, i, iImgLength)

Next i

PanImage.Refresh()

End Sub

Private Sub menuXOR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuXOR.Click

Dim r, g, b As Byte

If (iWmColor < 24) Then statusWM = wmimage.CIMG_promote_24()

If (iWmColor < 24) Then statusImg = simage.CIMG_promote_24()

iWmLength = iWmWidth * 3

iImgLength = iImgWidth * 3

Dim arrImageData(iImgLength) As Byte

Dim arrWMarkData(iWmLength) As Byte

For i As Integer = 1 To iWmHeight - 1

statusWM = wmimage.CIMGLOW_get_raster(arrWMarkData, i, iWmLength)

statusImg = simage.CIMGLOW_get_raster(arrImageData, i, iImgLength)

Dim iXStart As Integer = 0

For j As Integer = iXStart To iWmWidth - 1

r = arrWMarkData(3 * j)

g = arrWMarkData(3 * j + 1)

b = arrWMarkData(3 * j + 2)

If ((r < RThredL) And (g < GThredL) And (b < BThredL)) Then

arrImageData(3 * j) = arrImageData(3 * j) Xor 255

arrImageData(3 * j + 1) = arrImageData(3 * j + 1) Xor 255

arrImageData(3 * j + 2) = arrImageData(3 * j + 2) Xor 255

End If

Next j

statusImg = simage.CIMGLOW_put_raster(arrImageData, i, iImgLength)

Next i

PanImage.Refresh()

End Sub

Private Sub menuColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuColor.Click

Dim r, g, b As Byte

If (iWmColor < 24) Then statusWM = wmimage.CIMG_promote_24()

If (iWmColor < 24) Then statusImg = simage.CIMG_promote_24()

iWmLength = iWmWidth * 3

iImgLength = iImgWidth * 3

Dim arrImageData(iImgLength) As Byte

Dim arrWMarkData(iWmLength) As Byte

For i As Integer = 0 To iWmHeight - 1

statusWM = wmimage.CIMGLOW_get_raster(arrWMarkData, i, iWmLength)

statusImg = simage.CIMGLOW_get_raster(arrImageData, i, iImgLength)

Dim iXStart As Integer = 0

For j As Integer = iXStart To iWmWidth - 1

r = arrWMarkData(3 * j)

g = arrWMarkData(3 * j + 1)

b = arrWMarkData(3 * j + 2)

If ((r <= RThredL) Or (g <= GThredL) Or (b >= BThredL)) Then

If ((arrImageData(3 * j) <> 0) Or (arrImageData(3 * j + 1) <> 0) Or (arrImageData(3 * j + 2) <> 0)) Then

arrImageData(3 * j) = arrWMarkData(3 * j)

arrImageData(3 * j + 1) = arrWMarkData(3 * j + 1)

arrImageData(3 * j + 2) = arrWMarkData(3 * j + 2)

End If

End If

Next j

statusImg = simage.CIMGLOW_put_raster(arrImageData, i, iImgLength)

Next i

PanImage.Refresh()

End Sub

Private Sub menuMergeFunc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuMergeFunc.Click

If (iWmColor < 24) Then statusWM = wmimage.CIMG_promote_24()

If (iWmColor < 24) Then statusImg = simage.CIMG_promote_24()

statusImg = simage.CIMG_merge_bitmap(wmimage, 2, 0, 0)

PanImage.Refresh()

End Sub

End Class

Category:

Online Demo