Posted by Engr. Rajib Roy on 2/22/2010
| On this article we will place an Watermark on an Image. It is a very small coding and written in ASP.net VB. |
| To start the project lets make a form like followings |
![]() |
| Through browse button you can select an image where you want to place the watermark. When you click on Button the watermarked image will be displayed on image. |
| On button click event write the following code |
|
Imports System.Drawing Imports System.Drawing.Imaging
PartialClass _Default Inherits System.Web.UI.Page
Protected Sub cmdWatermark_Click(ByVal senderAs Object,ByVal e As System.EventArgs) Handles cmdWatermark.Click FileField.PostedFile.SaveAs(System.AppDomain.CurrentDomain.BaseDirectory() & "Images/" & FileField.FileName)
Dim newStrFileNameAs String Dim FilePath As String = System.AppDomain.CurrentDomain.BaseDirectory() &"Images/" & FileField.FileName Dim bmp As Bitmap = System.Drawing.Image.FromFile(FilePath) Dim strWatermarkAs String = Chr(169) ' Set the text of your Watermark
bmp = System.Drawing.Image.FromFile(FilePath)
Dim canvas As Graphics = Graphics.FromImage(bmp)
Dim StringSizeFAs SizeF, DesiredWidthAs Single, wmFont As Font, RequiredFontSizeAs Single, Ratio As Single
newStrFileName = System.AppDomain.CurrentDomain.BaseDirectory() &"Images/New" & FileField.FileName
wmFont = New Font("Verdana", 6, FontStyle.Bold)
DesiredWidth = bmp.Width * 0.5
StringSizeF = canvas.MeasureString(strWatermark, wmFont)
Ratio = StringSizeF.Width / wmFont.SizeInPoints
RequiredFontSize = DesiredWidth / Ratio
wmFont = New Font("Verdana", RequiredFontSize, FontStyle.Bold)
canvas.DrawString(strWatermark, wmFont, New SolidBrush(Color.FromArgb(100, 255, 255, 255)), 5, 5)
'canvas.DrawString(strWatermark, wmFont, New SolidBrush(Color.FromArgb(180, 255, 255, 255)), 0, 0)
bmp.Save(newStrFileName, ImageFormat.Jpeg) Image1.ImageUrl = "Images/New" & FileField.FileName
canvas.dispose()
bmp.dispose()
End Sub EndClass
|
| Run the project and select and image for watermark |
![]() |
| I hope the project will be helpful to you for placing watermark. |
| Download the source code. |
| About Author: | ||
![]() |
I am Rajib Roy, born in Bangladesh & now staying in UAE and working in a British company as a Sr. Programmer. | ![]() |
1 Responses
Leave a Reply
Nice Tutorial! Thanks