Bonjour,

je débute en programmation et je rencontre un problème pour afficher un rectangle après rotation ou translation : l'image est tronquée et on ne voit que la partie inscrite dans le rectangle avant rotation.

Voici mon code (soyez indulgents) , je ne sais pas beaucoup de choses, apparemment le problème vient de
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
           e.Graphics.Transform = myMatrix
            path.Transform(myMatrix)
pouvez-vous m'expliquer le rôle du path et pourquoi l'image est tronquée après transformation

Si vous avez qq minutes à me consacrer...Merci

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
Option Explicit On
Imports System.Drawing.Drawing2D
Public Class ClsEssai
    Inherits System.Windows.Forms.Control
    'Met en surbrillance selection
    Private selectionForeColor As Color = Color.Yellow
    'BackStore du ForeColor pour retablir apres deselection
    Private oldForeColor As Color
 
    Public Sub New()
        ' Cet appel est requis par le Concepteur Windows Form.
 
 
        oldForeColor = Me.ForeColor
 
        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
    End Sub
 
    Private shape As ShapeType = ShapeType.Rectangle
    Private path As GraphicsPath
    ' le type de shapes supporte par ce controle.
    Public Enum ShapeType
        Rectangle
    End Enum
 
    Public Property Type() As ShapeType
        Get
            Return shape
        End Get
        Set(ByVal value As ShapeType)
            shape = value
            RefreshPath()
            Me.Invalidate()
        End Set
    End Property
 
 
 
    ' Cree le GraphicsPath correspondant pour shape, 
    ' et l'affecte à la prop Region du controle .
    ' Rappel: le Region d'un controle est sa zone interactive
    Private rectangle1 As Rectangle 'rajout
    Private Sub RefreshPath()
        If path IsNot Nothing Then path.Dispose()
        path = New GraphicsPath()
        Select Case shape
            Case ShapeType.Rectangle
                path.AddRectangle(Me.ClientRectangle) 'avant
        End Select
        Me.Region = New Region(path)
    End Sub
 
 
    Dim myPen As New Pen(Color.Blue, 1)
    Dim myPen2 As New Pen(Color.Red, 1)
 
 
    ' Redefinition  OnResize
    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        MyBase.OnResize(e)
        RefreshPath()
        Me.Invalidate()
        Form1.Label1.BackColor = Color.Red
    End Sub
 
 
 
    ' Redefinition  OnPaint
    'Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    '  MyBase.OnPaint(e)
    '   Form1.Label1.BackColor = color.Red
    'Dim myPen As New Pen(Color.Blue, 1)
    ' Dim myPen2 As New Pen(Color.Red, 1)
 
 
    ' Draw the rectangle to the screen before applying the transform.
    '    e.Graphics.DrawRectangle(myPen, 150, 50, 200, 100)
 
    ' Create a matrix and rotate it 45 degrees.
    'Dim myMatrix As New Matrix
    '    myMatrix.Rotate(45, MatrixOrder.Append)
 
    ' Draw the rectangle to the screen again after applying the
    ' transform.
    '    e.Graphics.Transform = myMatrix
    '   e.Graphics.DrawRectangle(myPen2, 150, 50, 200, 100)
 
    ' End Sub
 
 
    Dim toto As Boolean
    Dim tata As Boolean
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        Form1.Label1.BackColor = Color.Red
        If path IsNot Nothing Then
            Dim shapeBrush As New SolidBrush(Me.BackColor)
            Dim shapePen As New Pen(Me.ForeColor, 5)
            Dim Pen As New Pen(Color.Red, 1)
            If toto Then
                e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                e.Graphics.FillPath(shapeBrush, path)
                e.Graphics.DrawPath(shapePen, path)
            End If
            hbwait(500)
 
            Dim rotatePoint As New PointF(Me.Left + Me.Width / 2, Me.Top + Me.Height / 2)
 
            ' Create a matrix and rotate it 45 degrees.
            Dim myMatrix As New Matrix
            ' myMatrix.RotateAt(45, rotatePoint, MatrixOrder.Append)
            myMatrix.Translate(10, 5)
 
 
            ' Draw the rectangle to the screen again after applying the
            ' transform.
            e.Graphics.Transform = myMatrix
            path.Transform(myMatrix)
            RefreshPath()
            ' e.Graphics.DrawPath(myPen, path)
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            e.Graphics.FillPath(shapeBrush, path)
            e.Graphics.DrawPath(Pen, path)
 
            shapePen.Dispose()
            shapeBrush.Dispose()
        End If
 
    End Sub
 
    Function Rotation(ByVal AngleDegre As Integer)
 
        Dim G As Graphics
 
        'on mémorise la longueur de l'image
        'je pars de l'hypothèse que j'utilise une image carré
        Dim Largeur As Integer = Me.Width
        Dim Hauteur As Integer = Me.Height
 
        'on mémorise l'image présente dans la picture box
        Dim IMG = Me
 
        'définition de la picturebox comme espace de dessin
        G = IMG.CreateGraphics()
 
        'on efface 
        G.Clear(Me.BackColor)
 
        'on transforme l'angle en radians
        Dim Angle As Double = AngleDegre / 180 * Math.PI
 
        'on calcule les nouvelles coord X et Y de l'image 
        Dim PosX As Single = Math.Sqrt(2) * (Largeur) * Math.Sin(Angle / 2) * Math.Cos(Math.PI / 4 + Angle / 2)
        Dim PosY As Single = -Math.Sqrt(2) * (Largeur) * Math.Sin(Angle / 2) * Math.Cos(Math.PI / 4 - Angle / 2)
 
        'on fait la rotation
        G.RotateTransform(AngleDegre)
 
        'on dessine l'image rotée aux nouvelles coordonnées
        Dim Pen As New Pen(Color.Red, 1)
        G.DrawRectangle(Pen, PosX, PosY, Largeur, Hauteur)
 
        'on libère !
        G.Dispose()
 
    End Function
 
    'Public Sub RotateAtExample(ByVal e As PaintEventArgs)
    'Dim myPen As New Pen(Color.Blue, 1)
    ' Dim myPen2 As New Pen(Color.Red, 1)
    ' Dim rotatePoint As New PointF(150.0F, 50.0F)
 
    ' Draw the rectangle to the screen before applying the
    ' transform.
    '    e.Graphics.DrawRectangle(myPen, 150, 50, 200, 100)
 
    ' Create a matrix and rotate it 45 degrees.
    ' Dim myMatrix As New Matrix
    '     myMatrix.RotateAt(45, rotatePoint, MatrixOrder.Append)
 
    ' Draw the rectangle to the screen again after applying the
    ' transform.
    '   e.Graphics.Transform = myMatrix
    '    e.Graphics.DrawRectangle(myPen2, 150, 50, 200, 100)
    'End Sub
 
End Class
 
Option Explicit On
Imports Microsoft.VisualBasic.PowerPacks
Imports System.Drawing.Drawing2D
Public Class Form1
    Private shp As ClsPaquet
    Dim rect As ClsEssai
    Dim numero As Integer
 
    Public Sub New()
 
        ' Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()
        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
 
        'Affecte le ContextMenuStrip au Form
        Me.ContextMenuStrip = Me.ctxMenuStripShapes
        Me.Panel1.Dock = DockStyle.Fill
        Me.Panel1.BackColor = Color.WhiteSmoke
        'Active le srolling
        Me.Panel1.AutoScroll = True
    End Sub
 
 
    Private Sub AddRectangle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctxMnuAddRectangle.Click
        shp = New ClsPaquet
        shp.Type = ClsPaquet.ShapeType.Rectangle
        shp.Width = 100
        shp.Height = 100
        shp.ForeColor = Color.Black
        'ajoute à  Panel1
        Me.Panel1.Controls.Add(shp)
        Me.Text = (Me.Panel1.Controls.Count).ToString
 
    End Sub
 
 
    'Enfin le  ConTextMenuStrip "mnuShape" propose 4 options. 
    '1ere , sur click autorise l'user à changer couleur 
    'de remplissage avec boite color dialog.
    'Le code retrouve le control shape actif avec la prop 
    'SourceControl du ContextMenuStrip
    Private Sub mnuColorChange_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuFillColorChange.Click
        Dim colorDlg As New ColorDialog
        If colorDlg.ShowDialog() = DialogResult.OK Then
            Me.mnuShape.SourceControl.BackColor = colorDlg.Color
        End If
    End Sub
    '2eme  option autorise l'user à supprimer le shape courant selectionne.
    Private Sub mnuRemoveShape_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuRemoveShape.Click
        Dim ctrl As New Control
        ctrl = Me.mnuShape.SourceControl
        'ctrl = Me
        'ctrl.Parent.Controls.Remove(Me)
        Panel1.Controls.Remove(ctrl)
    End Sub
    '2eme  option et 4eme option pour Z-Order.
    Private Sub mnuBringToFront_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuBringToFront.Click
        Dim ctrl As New Control
        ctrl = Me.mnuShape.SourceControl
        ctrl.BringToFront()
    End Sub
    Private Sub mnuSendToBack_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuSendToBack.Click
        Dim ctrl As New Control
        ctrl = Me.mnuShape.SourceControl
        ctrl.SendToBack()
    End Sub
    '- Click : selection du control position.
    '- Right-Click : affiche context menu, qui prevoit les option 
    ' -Delete Objet 
    ' -Change FillColor
    ' -BringToFront
    ' -SendToBack
    '- Click Coin Bas-Droit :redimensionnenement
 
    ' This example creates a PictureBox control on the form and draws to it. 
    ' This example assumes that the Form_Load event handler method is connected 
    ' to the Load event of the form.
 
    ' This example creates a PictureBox control on the form and draws to it. 
    ' This example assumes that the Form_Load event handler method is connected 
    ' to the Load event of the form.
    Private pictureBox2 As PictureBox
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
    End Sub 'Form1_Load
 
 
    Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        ' Create a local version of the graphics object for the PictureBox.
        Dim g As Graphics = e.Graphics
 
        ' Draw a string on the PictureBox.
        g.DrawString("This is a diagonal line drawn on the control", _
            New Font("Arial", 10), Brushes.Red, New PointF(30.0F, 30.0F))
        ' Draw a line in the PictureBox.
        g.DrawLine(System.Drawing.Pens.Red, PictureBox2.Left, _
            PictureBox2.Top, PictureBox2.Right, PictureBox2.Bottom)
    End Sub 'pictureBox1_Paint
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        pictureBox2 = New PictureBox
        ' Add the PictureBox control to the Form.
        Me.Panel1.Controls.Add(pictureBox2)
        ' Dock the PictureBox to the form and set its background to white.
        pictureBox2.Dock = DockStyle.Fill
        pictureBox2.BackColor = Color.White
        ' Connect the Paint event of the PictureBox to the event handler method.
        AddHandler pictureBox2.Paint, AddressOf Me.pictureBox1_Paint
    End Sub
 
    ' Private Sub RotateExample(ByVal e As PaintEventArgs)
    'Dim myPen As New Pen(Color.Blue, 1)
    ' Dim myPen2 As New Pen(Color.Red, 1)
 
    ' Draw the rectangle to the screen  applying the transform.
    '    e.Graphics.DrawRectangle(myPen, 150, 50, 200, 100)
 
    ' Create a matrix and rotate it 45 degrees.
    ' Dim myMatrix As New Matrix
    '    myMatrix.Rotate(45, MatrixOrder.Append)
 
    ' Draw the rectangle to the screen again after applying the
    ' transform.
    '    e.Graphics.Transform = myMatrix
    '    e.Graphics.DrawRectangle(myPen2, 150, 50, 200, 100)
    ' End Sub
 
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        rect = New ClsEssai
        rect.Type = ClsEssai.ShapeType.Rectangle
        rect.Width = 200
        rect.Height = 200
        rect.Top = 0
        rect.Left = 0
        rect.ForeColor = Color.Black
        Dim lab As Label
        lab = New Label
 
        lab.BackColor = Color.Red
        'ajoute à  Panel1
        Me.Panel1.Controls.Add(rect)
        Me.Panel1.Controls.Add(lab)
 
        Me.Text = (Me.Panel1.Controls.Count).ToString
 
    End Sub
 
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        shp = New ClsPaquet
        shp.Type = ClsPaquet.ShapeType.Rectangle
        shp.Width = 100
        shp.Height = 100
        shp.ForeColor = Color.Black
        'ajoute à  Panel1
        Me.Panel1.Controls.Add(shp)
        Me.Text = (Me.Panel1.Controls.Count).ToString
 
 
    End Sub
End Class