1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
Imports System.Drawing.Drawing2D
Public Class Form1
Private angle As Single
Public Sub New()
' Cet appel est requis par le concepteur.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
Pic.Size = New Size(300, 250)
Pic.SizeMode = PictureBoxSizeMode.StretchImage
NumericUpDown1.Maximum = 360
NumericUpDown1.Minimum = -360
NumericUpDown1.Value = 0
angle = 0
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DrawStringRotated()
End Sub
Private Sub DrawStringRotated()
If String.IsNullOrEmpty(TextBox1.Text) Then Return
Dim drawString As String = TextBox1.Text
Dim drawFont As New Font("Arial", 72, FontStyle.Bold)
Dim drawBrush As New SolidBrush(Color.LightSlateGray)
Dim sizeString As SizeF = GetSizeString(drawString, drawFont)
Dim drawFormat As New StringFormat
Dim textArea As Rectangle 'rectangle englobant de la taille reelle
'le bitmap convoité assez grand pour contenir le texte
'meme quand il tourne
Dim w As Integer = Math.Max(sizeString.Width, sizeString.Height)
Dim bmp As New Bitmap(w, w)
Using gr As Graphics = Graphics.FromImage(bmp)
gr.ResetTransform()
gr.Clear(Color.White)
gr.SmoothingMode = SmoothingMode.HighQuality
drawFormat.FormatFlags = StringFormatFlags.NoWrap 'pas de retour à la ligne
textArea = New Rectangle(0, 0, sizeString.Width, sizeString.Height)
'on decale l'origine du rectangle englobant car l'origine des coordonnees sera translaté
textArea.Offset(-CInt(sizeString.Width / 2), -CInt(sizeString.Height / 2))
gr.TranslateTransform(sizeString.Width / 2, bmp.Height / 2)
gr.RotateTransform(angle)
gr.DrawString(drawString, drawFont, drawBrush, textArea, drawFormat)
gr.DrawRectangle(Pens.Red, textArea)
drawFont.Dispose()
drawBrush.Dispose()
End Using
Pic.Image = bmp
End Sub
Private Function GetSizeString(ByVal s As String, ByVal fnt As Font) As SizeF
Dim sz As SizeF
Dim bm As Bitmap = New Bitmap(2, 2) 'peu importe le bitmpa,l'essentiel est d'avoir un objet graphics
Using gr As Graphics = Graphics.FromImage(bm)
sz = gr.MeasureString(s, fnt) 'taille reelle
End Using
Return sz
End Function
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
angle = NumericUpDown1.Value
DrawStringRotated()
End Sub
End Class |
Partager