@bobnoordam

Creating a superimposed centered watermarked image at runtime

''' <summary>
''' Watermarks the file sOrinignalfilehandle with the content of sWatermarkfilehandle. The resulting
''' watermarked picture is written to sOriginalfilehandle.
'''
''' Returns 1 for success, -1 if the operation can not be performed because of size mismatches
''' </summary>
''' <param name="sOriginalfilehandle"></param>
''' <param name="sWatermarkfilehandle"></param>
''' <remarks></remarks>
Private Function WaterMarkFile(ByVal sOriginalfilehandle As String, ByVal sWatermarkfilehandle As String) As Integer
  
  
    Dim oCanvas As Bitmap
  
    Using oFs As New FileStream(sOriginalfilehandle, FileMode.Open, FileAccess.Read)
        Using oSourceimage As Bitmap = Bitmap.FromStream(oFs)
            Using oWatermark As Bitmap = Bitmap.FromFile(sWatermarkfilehandle)
  
                ' --- Watermark must be equal or smaller then source dimensions
                If oWatermark.Width > oSourceimage.Width Or oWatermark.Height > oSourceimage.Height Then
                    Return -1
                End If
                ' //
  
                ' --- Center watermark
                Dim xStart As Integer = (Int(oSourceimage.Width - oWatermark.Width) / 2) - 1
                Dim yStart As Integer = (Int(oSourceimage.Height - oWatermark.Height) / 2) - 1
                ' //
  
                ' --- Perform watermarking
                For x = xStart To xStart + oWatermark.Width - 1
                    For y = yStart To yStart + oWatermark.Height - 1
                        Dim oWatermarkpixel As Color = oWatermark.GetPixel(x - xStart, y - yStart)
                        If oWatermarkpixel.A > 0 Then ' --- There is watermark alpha present
                            Dim oOriginalpixel As Color = oSourceimage.GetPixel(x, y)
                            Dim PixelMultiplier As Single = 1 - CSng(((1 - oWatermarkpixel.GetBrightness)) * 0.25)
                            Dim NewPixel As Color = Color.FromArgb(oOriginalpixel.A, CInt(oOriginalpixel.R * PixelMultiplier), CInt(oOriginalpixel.G * PixelMultiplier), CInt(oOriginalpixel.B * PixelMultiplier))
                            oSourceimage.SetPixel(x, y, NewPixel)
                        End If
                    Next
                Next
                ' //
  
                ' --- oSourceimage is locked by GDI+, create clone to save
                oCanvas = oSourceimage.Clone
                ' //
  
            End Using
        End Using
    End Using
  
    oCanvas.Save(sOriginalfilehandle, System.Drawing.Imaging.ImageFormat.Png)
  
    Return 1
  
En