I'm currently working on a project, but need the codes for watermarking the image.
Can somebody help?
agrothe 26 Junior Poster
This should get you on your way. http://www.codeproject.com/KB/GDI-plus/TransparentImageWatermark.aspx
magdyalgamal 0 Newbie Poster
Public Class Form1
Dim x, y As Integer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
x = 0
y = 0
cboOpacity.Items.AddRange(New Object() {"100%", "75%", "50%", "25%", "10%"})
cboOpacity.SelectedIndex = 2
Tmp = Application.StartupPath & "\New\"
If Not Directory.Exists(Tmp) Then IO.Directory.CreateDirectory(Tmp)
End Sub
Dim Tmp As String
Dim MyWatermarkColor As System.Drawing.Color
Dim myBrush As Brush
Dim opac As Integer = 0
Public Function watermark(ByVal text As String, ByRef fnt As System.Windows.Forms.FontDialog, Optional ByVal loc As Integer = 0) As Boolean
Try
'Dim FilePath As String = strFileName
Dim bmp As Bitmap = Pic.BackgroundImage
Dim strWatermark As String = text
Dim canvas As Graphics = Graphics.FromImage(bmp)
' Determine the opacity of the watermark
myBrush = New SolidBrush(Color.FromArgb(opac, MyWatermarkColor))
'place(loc, bmp, RequiredFontSize)
If Text_RadioBtn.Checked = True Then canvas.DrawString(strWatermark, fnt.Font, myBrush, x, y)
'If Picture_RadioBtn.Checked = True Then canvas.DrawImage(Pic2.BackgroundImage, x, y)
If Picture_RadioBtn.Checked = True Then canvas.DrawImage(Pic2.BackgroundImage, x, y)
bmp.SetResolution(96, 96)
bmp.Save(Tmp & Fil.FileName, ImageFormat.Jpeg)
canvas.Dispose()
bmp.Dispose()
Return True
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Public Sub place(ByVal place As Integer, ByRef img As System.Drawing.Image)
Dim RequiredFontSize As Single ', Ratio As Single
RequiredFontSize = Fnt.Font.Size
Select Case place
Case 1 ' left top
x = 0
y = 0
Case 2 ' top mid
x = Math.Max(CInt((img.Width / 2) - (RequiredFontSize / 2)), 0)
y = 0
Case 3 'right top
x = Math.Max(CInt(img.Width - RequiredFontSize), 0)
y = 0
Case 4 'left down
x = 0
y = Math.Max(CInt(img.Height - RequiredFontSize), 0)
Case 5 'mid down
x = Math.Max(CInt((img.Width / 2) - (RequiredFontSize / 2)), 0)
y = Math.Max(CInt(img.Height - RequiredFontSize), 0)
Case 6 ' right down
x = Math.Max(CInt(img.Width - RequiredFontSize), 0)
y = Math.Max(CInt(img.Height - RequiredFontSize), 0)
End Select
End Sub
Private Sub Watermark_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Watermark_Btn.Click
place(3, Pic.BackgroundImage)
If watermark(Watermark_TextBox.Text, Fnt) Then
' Clipboard.SetDataObject(FromFile(tmp))
' Pic.BackgroundImage = Clipboard.GetDataObject.GetData(DataFormats.Bitmap)
' Clipboard.Clear()
File.Copy(Tmp, Tmp & "x", True)
Pic.BackgroundImage = FromFile(Tmp & "x")
End If
End Sub
Dim Fnt As New System.Windows.Forms.FontDialog
Private Sub Font_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Font_Btn.Click
If Fnt.ShowDialog() = DialogResult.Cancel Then Exit Sub
Watermark_TextBox.Font = Fnt.Font
End Sub
Dim Fil As New System.Windows.Forms.OpenFileDialog
Dim Clr As New System.Windows.Forms.ColorDialog
Private Sub Color_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Color_Btn.Click
If Clr.ShowDialog() = DialogResult.Cancel Then Exit Sub
Watermark_TextBox.ForeColor = Clr.Color
myWatermarkColor = Clr.Color
End Sub
Private Function Mrg() As Boolean
Dim Circle As Bitmap = Pic.BackgroundImage
Dim MergedBMP As Bitmap = Pic2.BackgroundImage
Dim g As Graphics = Graphics.FromImage(Circle)
MergedBMP.MakeTransparent(Color.Blue)
g.DrawImage(MergedBMP, 0, 0)
g.Dispose()
Pic.Image = Circle
End Function
Private Sub Merge_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Merge_Btn.Click
If Mrg() Then Pic.BackgroundImage = FromFile("c:\new\5.jpeg")
End Sub
Private Sub Front_Picture_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Front_Picture_Btn.Click
If Fil.ShowDialog() = DialogResult.Cancel Then Exit Sub
Pic2.BackgroundImage = FromFile(Fil.FileName)
End Sub
Private Sub Back_Picture_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Back_Picture_Btn.Click
If Fil.ShowDialog() = DialogResult.Cancel Then Exit Sub
Pic.BackgroundImage = FromFile(Fil.FileName)
End Sub
Private Sub Pic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic.MouseDown
y = e.Location.Y
x = e.Location.X
Label3.Text = y
Label4.Text = x
End Sub
Private Sub cboOpacity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboOpacity.SelectedIndexChanged
Dim sOpacity As String = cboOpacity.Text
Select Case (sOpacity)
Case "100%"
opac = 255 ' 1 * 255
Case "75%"
opac = 191 ' .75 * 255
Case "50%"
opac = 127 ' .5 * 255
Case "25%"
opac = 64 ' .25 * 255
Case "10%"
opac = 25 ' .10 * 255
Case Else
opac = 127 ' default at 50%; .5 * 255
End Select
End Sub
End Class
Public Class Clndr
Private Sub Clndr_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CallDate(Date.Today)
End Sub
Dim FirstDayOfMonth As Date
Dim FirstDayOfWeek As DateTime
Private Sub CallDate(ByVal MyDate As Date)
Today_Date.Text = MyDate
Month_Number.Text = Month(MyDate)
Month_Name.Text = MonthName(Month(MyDate))
Day_Name.Text = WeekdayName(Weekday(MyDate))
Days_In_Month.Text = DateTime.DaysInMonth(Year(MyDate), Month(MyDate))
Day_Number.Text = MyDate.Day
FirstDayOfMonth = DateSerial(Year(MyDate), Month(MyDate), 1)
First_Day_Of_Month.Text = WeekdayName(Weekday(FirstDayOfMonth))
FirstDayOfMonth = DateSerial(Year(MyDate), 1, 1)
First_Day_Of_Year.Text = WeekdayName(Weekday(FirstDayOfMonth))
FirstDayOfWeek = DateAdd("d", 0 - MyDate.DayOfWeek, MyDate)
First_Day_Of_Week.Text = WeekdayName(Weekday(FirstDayOfWeek))
Day_Of_Week_No.Text = Weekday(FirstDayOfMonth)
End Sub
Private Sub Creat_Control_Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Creat_Control_Btn.Click
Dim PicBox As PictureBox
PicBox = New PictureBox()
PicBox.BackgroundImage = My.Resources.السبت
PicBox.Top = 150
PicBox.Left = 0
PicBox.SizeMode = PictureBoxSizeMode.AutoSize
PicBox.BackgroundImageLayout = ImageLayout.Center
PicBox.BorderStyle = BorderStyle.FixedSingle
Controls.Add(PicBox)
Dim bmp As Bitmap = Pic.BackgroundImage
Dim canvas As Graphics = Graphics.FromImage(bmp)
Dim Lft As Single = Math.Max(CInt(Pic.Width - (PicBox.Width / 2)), 0) + 20
Dim Tp As Single = 0
For x = 1 To 7
Select Case x
Case 1
PicBox.Image = My.Resources.الأحد
Case 2
PicBox.Image = My.Resources.الإثنين
Case 3
PicBox.Image = My.Resources.الثلاثاء
Case 4
PicBox.Image = My.Resources.الأربعاء
Case 5
PicBox.Image = My.Resources.الخميس
Case 6
PicBox.Image = My.Resources.الجمعة
Case 7
PicBox.Image = My.Resources.السبت
End Select
canvas.DrawImage(PicBox.Image, lft, 0)
Lft -= PicBox.Width - 1
Next
Lft = Math.Max(CInt(Pic.Width - (PicBox.Width / 2)), 0) + 19
Tp = PicBox.Height - 2
Dim Clmn As Integer = 0
For x = 1 To Weekday(FirstDayOfMonth)
PicBox.Image = My.Resources.Blank
canvas.DrawImage(PicBox.Image, Lft, Tp)
Lft -= PicBox.Width - 1
Clmn += 1
Next
For x = 1 To DateTime.DaysInMonth(Year(Date.Today), Month(Date.Today))
If x = Today.Day Then
PicBox.Image = My.Resources.red
canvas.DrawImage(PicBox.Image, Lft, Tp)
End If
Select Case x
Case 1
PicBox.Image = My.Resources._1
Case 2
PicBox.Image = My.Resources._2
Case 3
PicBox.Image = My.Resources._3
Case 4
PicBox.Image = My.Resources._4
Case 5
PicBox.Image = My.Resources._5
Case 6
PicBox.Image = My.Resources._6
Case 7
PicBox.Image = My.Resources._7
Case 8
PicBox.Image = My.Resources._8
Case 9
PicBox.Image = My.Resources._9
Case 10
PicBox.Image = My.Resources._10
Case 11
PicBox.Image = My.Resources._11
Case 12
PicBox.Image = My.Resources._12
Case 13
PicBox.Image = My.Resources._13
Case 14
PicBox.Image = My.Resources._14
Case 15
PicBox.Image = My.Resources._15
Case 16
PicBox.Image = My.Resources._16
Case 17
PicBox.Image = My.Resources._17
Case 18
PicBox.Image = My.Resources._18
Case 19
PicBox.Image = My.Resources._19
Case 20
PicBox.Image = My.Resources._20
Case 21
PicBox.Image = My.Resources._21
Case 22
PicBox.Image = My.Resources._22
Case 23
PicBox.Image = My.Resources._23
Case 24
PicBox.Image = My.Resources._24
Case 25
PicBox.Image = My.Resources._25
Case 26
PicBox.Image = My.Resources._26
Case 27
PicBox.Image = My.Resources._27
Case 28
PicBox.Image = My.Resources._28
Case 29
PicBox.Image = My.Resources._29
Case 30
PicBox.Image = My.Resources._30
Case 31
PicBox.Image = My.Resources._31
End Select
Clmn += 1
canvas.DrawImage(PicBox.Image, Lft, Tp)
If Clmn = 8 Then
Lft = Math.Max(CInt(Pic.Width - (PicBox.Width / 2)), 0) + 19
Tp += PicBox.Height - 2
Clmn = 1
Lft -= PicBox.Width - 1
Else
Lft -= PicBox.Width - 1
End If
Next
bmp.Save("picName.jpg", ImageFormat.Jpeg)
canvas.Dispose()
bmp.Dispose()
File.Copy("picName.jpg", "picName_Copy.jpg", True)
Pic.BackgroundImage = Image.FromFile("picName_Copy.jpg")
End Sub
End Class
Edited by mike_2000_17 because: Fixed formatting
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.