Hi- Does anyone have any good code to watermark an image in vb.net? I guess this would be effectively the same as overlaying a 2nd image on top of an existing image.

Thanks,
Eric

Imports

Imports System.Drawing.Image
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D

Class level declaration

Dim x, y As Integer

Watermark method

Public Sub watermark(ByVal strFileName As String, ByVal text As String, ByVal loc As Integer, ByRef fnt As System.Windows.Forms.FontDialog)
        Dim FilePath As String = strFileName
        Dim bmp As Bitmap = System.Drawing.Image.FromFile(FilePath)
        Dim strWatermark As String = text
        Dim canvas As Graphics = Graphics.FromImage(resize(bmp, 130, 130))
        Dim wmFont As Font, RequiredFontSize As Single ', Ratio As Single 
        Dim tmp As String
        tmp = FilePath.Substring(FilePath.LastIndexOf("\"))
        tmp = tmp.Remove(tmp.IndexOf("\"), 1)
        IO.Directory.CreateDirectory("C:/New")
        tmp = "C:/New/" & tmp
        wmFont = fnt.Font
        place(loc, bmp, RequiredFontSize)
        canvas.DrawString(strWatermark, wmFont, New SolidBrush(fnt.Color), x, y)
             bmp.SetResolution(96, 96)
        bmp.Save(tmp, ImageFormat.Jpeg)
        canvas.Dispose()
        bmp.Dispose()
    End Sub

place method will return the location of watermark..

Public Sub place(ByVal place As Integer, ByRef img As System.Drawing.Image, ByRef fntsize As Integer)
        Select Case place
            Case 1
                x = 0
                y = 0
            Case 2
                x = Math.Max(CInt((img.Width / 2) - (fntsize / 2)), 0)
                y = 0
            Case 3
                x = Math.Max(CInt(img.Width - fntsize), 0)
                y = 0
            Case 4
                x = 0
                y = Math.Max(CInt(img.Height - fntsize), 0)
                x = 100
                y = 800

            Case 5
                x = Math.Max(CInt((img.Width / 2) - (fntsize / 2)), 0)
                y = Math.Max(CInt(img.Height - fntsize), 0)
                x = 100
                y = 800
            Case 6
                x = Math.Max(CInt(img.Width - fntsize), 0)
                y = Math.Max(CInt(img.Height - fntsize), 0)
        End Select
    End Sub

I guess the place method might not return proper location....change the code according to what u require......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.