Monday 4 May 2009

Creating Image Watermarks in VB.NET

Fancy making your own batch watermarking application? Here's a function that'll get you started. I originally adapted this from some code that was serving images in asp.net watermarked at runtime using a http handler. That was pretty nifty but I really needed it to watermark images for some automated uploading to facebook.


Public Function AddWaterMark(ByVal theImage As Image) As Image


Dim canvas As Graphics


'create a new bmp the size of the passed image
Dim bmpNew As Bitmap = New Bitmap(theImage.Width, theImage.Height)
'create a canvas
canvas = Graphics.FromImage(bmpNew)


'draw the passed image onto the canvas
canvas.DrawImage(theImage, New Rectangle(0, 0, _
bmpNew.Width, bmpNew.Height), 0, 0, _
theImage.Width, theImage.Height, GraphicsUnit.Pixel)
theImage = bmpNew



Dim overlayImg As Bitmap


'make the overlay image
overlayImg = Drawing.Image.FromFile("C:\someplace\mywatermark.png", True)

'draw the overlay onto the top left corner
canvas.DrawImage(overlayImg, 0, 0, overlayImg.Width, overlayImg.Height)

Dim tempImage As Image = bmpNew
AddWaterMark = tempImage



End Function

No comments: