01-11-15, 01:41 PM
الكود التالي يوضح كيف ترسم علامة مائية عبارة عن تكست علي سطح الفورم
كود :
Public Class Form1
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
If Me.Width < 0 AndAlso Me.Height < 0 Then
Return
End If
Dim rect As Rectangle = New Rectangle(0, 0, Me.Width, Me.Height)
Dim bmp As Bitmap = New Bitmap(CInt(Width), CInt(Height), Imaging.PixelFormat.Format32bppArgb)
Dim inclination As Integer = 315
If inclination < 0 Then
inclination = 0
End If
If inclination > 360 Then
inclination = 360
End If
Using g As Graphics = Graphics.FromImage(bmp)
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
g.Clear(Me.BackColor)
Dim watermark As String = "Visual Basic For Arab"
Dim maxFontSize As Integer = 70
Dim minFontSize As Integer = CInt(Me.Font.Size)
Dim maxWidth As Integer = bmp.Width
Dim controlFont As Font = Me.Font
Dim drawingFont As Font = CType(Nothing, Font)
Dim bestFitFont As Font = CType(Nothing, Font)
For i As Integer = minFontSize To maxFontSize Step 1
drawingFont = New Font(controlFont.Name, i, controlFont.Style)
Dim textSize As SizeF = g.MeasureString(watermark, drawingFont)
If maxWidth > CInt(textSize.Width) Then
bestFitFont = drawingFont
End If
Next
Dim watermarkTextSize As SizeF = g.MeasureString(watermark, bestFitFont)
g.TranslateTransform(bmp.Width / 2, bmp.Height / 2)
g.RotateTransform(inclination)
g.DrawString(watermark, bestFitFont, New SolidBrush(ControlPaint.LightLight(Me.BackColor)), -(watermarkTextSize.Width / 2), -(watermarkTextSize.Height / 2))
End Using
e.Graphics.DrawImage(bmp, 0, 0)
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
Invalidate()
End Sub
End Class
