'Visit http://www.laptrinhdotnet.com for more ASP.NET Tutorials
Imports System.IO
Imports System.Drawing
Namespace AddWatermarkTextOnUploaded
Public Class _Default
Inherits System.Web.UI.Page
#Region "Private Members"
Private FilePath As String = ""
Private FileFilter AsString = "gif,jpg,jpeg,png"
#End Region
#Region "Private Methods"
Protected FunctionUploadFile(ByVal Upload As FileUpload) As String
Dim fileName As String = ""
Dim fileExtension As String = ""
SetFilePath()
Dim bmpUpload As Image = Image.FromStream(Upload.PostedFile.InputStream)
fileExtension = Replace(Path.GetExtension(Upload.FileName), ".", "")
fileName = Upload.FileName.Substring(Upload.FileName.LastIndexOf("\\") + 1)
fileName = fileName.Substring(0, fileName.LastIndexOf(fileExtension)) & fileExtension
FilePath = FilePath + fileName
Using g As Graphics = Graphics.FromImage(bmpUpload)
Dim opacity AsInteger = 128
Dim brush AsNew SolidBrush(Color.FromArgb(opacity, Color.White))
Dim font AsNew Font("Arial", 16, FontStyle.Bold)
g.DrawString(txtWatermarkText.Text.Trim, font, brush, New PointF(bmpUpload.Width / 15, bmpUpload.Height / 15))
bmpUpload.Save(Path.Combine(FilePath))
imgPreview.ImageUrl = ResolveUrl("~/Upload/") & fileName
imgPreview.Visible = True
End Using
Return fileName
End Function
Private SubSetFilePath()
FilePath = MapPath("~/Upload/")
'Create Folder
If Not Directory.Exists(FilePath) Then
Directory.CreateDirectory(FilePath)
End If
End Sub
#End Region
#Region "Event Handles"
Private SubcmdUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) HandlescmdUpload.Click
If Page.IsValid Then
Try
Dim fileName AsString = ""
If FileUpload.PostedFile.FileName <> "" Then
fileName = UploadFile(FileUpload)
If fileName <> ""Then
lblMessage.Text = "Upload file Sucessfully"
lblMessage.ForeColor = System.Drawing.Color.Green
lblMessage.Visible = True
End If
End If
Catch ex AsException
lblMessage.Text = ex.Message
lblMessage.ForeColor = System.Drawing.Color.Red
lblMessage.Visible = True
Exit Sub
End Try
End If
End Sub
Private SubvalFile_ServerValidate(ByVal source As System.Object, ByVal args AsSystem.Web.UI.WebControls.ServerValidateEventArgs) Handles valFile.ServerValidate
Try
If NotFileUpload.PostedFile Is Nothing Then
If FileUpload.PostedFile.ContentLength > 0 Then
args.IsValid = True
Return
End If
End If
args.IsValid = False
Catch exc As Exception
End Try
End Sub
Private SubvalType_ServerValidate(ByVal source As System.Object, ByVal args AsSystem.Web.UI.WebControls.ServerValidateEventArgs) Handles valType.ServerValidate
Try
If Not(FileUpload.PostedFile Is Nothing) Then
lblMessage.Visible = False
If (FileUpload.PostedFile.ContentLength > 0) Then
Dim arr As String() = FileUpload.PostedFile.FileName.Split(Convert.ToChar("."))
Dim fileType As String = arr(arr.Length - 1).ToLower()
valType.ErrorMessage = "You must upload a file that is either a gif, jpg, jpeg, png"
If FileFilter <> ""And Not InStr("," & FileFilter.ToLower, "," & fileType.ToLower) = 0 Then
args.IsValid = True
Return
Else
args.IsValid = False
End If
args.IsValid = False
End If
End If
Catch exc As Exception
End Try
End Sub
#End Region
End Class
End Namespace