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 92 93 94 95 96
|
Public Class Form2
Private myImage As Bitmap = Nothing
Public Sub New()
' Cet appel est requis par le concepteur.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
Panel1.AutoScroll = True
PictureBox1.Location = New Point(0, 0)
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
PictureBox1.Image = My.Resources.Koala
TextBox1.Text = "So we don't need to go through all eight"
TextBox1.ForeColor = Color.Red
TextBox1.Font = New Font(TextBox1.Font.Name, 12, FontStyle.Bold)
TextBox2.Text = "What about for other polygons?"
TextBox2.ForeColor = Color.BlueViolet
TextBox2.Font = New Font(TextBox1.Font.Name, 12, FontStyle.Bold)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myImage = PictureBox1.Image
PrepareImage()
End Sub
Private Sub PrepareImage()
Using gr As Graphics = Graphics.FromImage(myImage)
Using txtBrush As New SolidBrush(TextBox1.ForeColor)
gr.DrawString(TextBox1.Text, TextBox1.Font, txtBrush, TextBox1.Location)
End Using
Using txtBrush As New SolidBrush(TextBox1.ForeColor)
gr.DrawString(TextBox2.Text, TextBox2.Font, txtBrush, TextBox2.Location)
End Using
End Using
'Refresh PictureBox
PictureBox1.Image = myImage
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
If myImage Is Nothing Then Return
' Let the user select page settings
Dim gr As Graphics = e.Graphics
Dim rect As Rectangle = e.PageBounds
rect = ImageRectangleFromSize(rect, myImage)
rect.Inflate(-20, -20)
myImage = New Bitmap(myImage, rect.Width, rect.Height)
gr.DrawImage(myImage, rect)
End Sub
'Rectangle du mode ZOOM extrait du code source .Net Framework source de PictureBox
Private Function ImageRectangleFromSize(ByVal args As Rectangle, ByVal img As Image) As Rectangle
Dim result As Rectangle = args
If (img IsNot Nothing) Then
Dim imageSize As Size = img.Size
Dim ratio As Single = Math.Min(args.Width / imageSize.Width, args.Height / imageSize.Height)
result.Width = CInt(imageSize.Width * ratio)
result.Height = CInt(imageSize.Height * ratio)
result.X = (args.Width - result.Width) / 2
result.Y = (args.Height - result.Height) / 2
End If
Return result
End Function
'Impression
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If myImage Is Nothing Then Return
PageSetupDialog1.Document = PrintDocument1
PageSetupDialog1.ShowDialog()
PrintDocument1.Print()
End Sub
'Verifie le positionnement du texte des 2 TextBox
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
TextBox1.Hide()
TextBox2.Hide()
Else
TextBox1.Show()
TextBox2.Show()
End If
End Sub
End Class |
Partager