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
|
System.Drawing.Drawing2D
Public Class UserControlImage
Private currentBmp As Bitmap
Private GraphicsControl As Graphics
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
LoadCurrentMap()
CenterBmp = New Point(Me.ImageWidth / 2, Me.ImageHeight / 2)
' recuper le graphics de la surface de dessin
GraphicsControl = Graphics.FromImage(currentBmp)
renderBitmap(GraphicsControl, currentBmp)
End Sub
Private Sub LoadCurrentMap()
currentBmp = New Bitmap(Me.Width, Me.Height)
' recuper le graphics de la surface de dessin
GraphicsControl = Graphics.FromImage(currentBmp)
renderBitmap(GraphicsControl, currentBmp)
CenterBmp = New Point(Me.ImageWidth / 2, Me.ImageHeight / 2)
End Sub
Private Sub renderBitmap(ByVal GraphicsControl As Graphics, ByVal cachedCurrentMap As Bitmap)
Dim ContextGraph As BufferedGraphicsContext = BufferedGraphicsManager.Current
ContextGraph.MaximumBuffer = New Size(cachedCurrentMap.Width, cachedCurrentMap.Height)
'-------- dimensioner le buffergraph --------
Dim BufferGraph As BufferedGraphics = ContextGraph.Allocate(GraphicsControl, New Rectangle(0, 0, cachedCurrentMap.Width, cachedCurrentMap.Height))
'-------- effacer fond --------
BufferGraph.Graphics.FillRectangle(Brushes.Beige, 0, 0, cachedCurrentMap.Width, cachedCurrentMap.Height)
GraphicsControl.FillRectangle(Brushes.Beige, 0, 0, cachedCurrentMap.Width, cachedCurrentMap.Height)
'-------- dessiner --------
For i = 0 To 1000 Step 10
BufferGraph.Graphics.DrawLine(Pens.Black, 0, 0, i * 10, 1000)
Next
'-------- envoyer dessin --------
BufferGraph.Render()
BufferGraph.Render(GraphicsControl)
BufferGraph.Dispose()
'-------- call to repaint --------
Me.Invalidate()
End Sub
Private Sub UserControlImage_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
' --------reload bmp --------
Me.LoadCurrentMap()
'-------- call to repaint --------
Me.Invalidate()
End Sub
'--------------- Panning the bmp ------------
Private dragDifferential As Point = Point.Empty
Private DownMouse As Boolean = False
Private ptDown As Point = New Point(-1, -1)
Private ptMove As Point = New Point(-1, -1)
Private Sub UserControlImage_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
ptDown = Me.PointToClient(MousePosition)
DownMouse = True
End Sub
Private Sub UserControlImage_Move(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Move
'-------- ignore if mouse button is not down --------
If (Not Me.DownMouse) Then Return
ptMove = Me.PointToClient(MousePosition)
'--------calculate change of mouse position --------
Dim current As Point = Me.dragDifferential
current.Offset((ptMove.X - ptDown.X), (ptMove.Y - ptDown.Y))
'-------- update dragDifferential--------
current = Me.dragDifferential
ptDown = ptMove
'-------- call to repaint --------
Me.Invalidate()
End Sub
Private Sub UserControlImage_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
DownMouse = False
'-------- update dragDifferential--------
Dim ptUp As Point = Me.PointToClient(MousePosition)
Me.dragDifferential = ptUp - ptDown
'-------- update Center Bmp--------
Me.LoadCurrentMap()
' --------reload bmp --------
Me.CenterBmp = ptUp
End Sub
'--------------- Zooming the bmp ------------
'
'Tracking the zoomDifferential
Private zoomDifferential As Double = 1
Private Sub UserControlImage_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
If (e.Delta > 0) Then
Me.zoomDifferential = 2 * Me.zoomDifferential
Me.Zoom += Zoom
ElseIf (e.Delta < 0) Then
Me.zoomDifferential = Me.zoomDifferential / 2
Me.Zoom -= Zoom
End If
' --------reload bmp --------
LoadCurrentMap()
'-------- call to repaint --------
Me.Invalidate()
End Sub
Private Sub UserControlImage_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If (Me.currentBmp Is Nothing) Then Return
Dim gr As Graphics = e.Graphics
'--------Centre des coord. au centre du bmp --------
gr.TranslateTransform(Me.CenterBmp.X, Me.CenterBmp.Y)
'--------is Moving the bmp --------
If Not Me.dragDifferential.IsEmpty Then
Dim diff As Point = dragDifferential - New Point(0, 0)
e.Graphics.TranslateTransform(diff.X, diff.Y)
End If
'--------is Zooming the bmp --------
If (Me.zoomDifferential <> 1) Then
Dim zoom As Double = Me.zoomDifferential
gr.ScaleTransform(zoom, zoom)
End If
'--------draw bmp centered (suite au centre decale) --------
Dim rect As Rectangle = New Rectangle(-Me.CenterBmp.X, -Me.CenterBmp.Y, Me.ImageWidth, Me.ImageHeight)
e.Graphics.DrawImage(Me.currentBmp, rect)
End Sub
' ----------------- properties------------------
Private m_centerBmp As Point = New Point(0, 0)
Public Property CenterBmp() As Point
Get
Return m_centerBmp
End Get
Set(ByVal value As Point)
m_centerBmp = value
End Set
End Property
Private m_Zoom = 5
Public Property Zoom() As Integer
Get
Return m_Zoom
End Get
Set(ByVal value As Integer)
m_Zoom = Math.Min(Math.Max(value, 1), 21)
End Set
End Property
' -----------------ReadOnly properties------------------
' -----------------largeur max & hauteur max du bmp à toi de les
' ----------------suivant tes besoins -----------------
Public ReadOnly Property ImageWidth() As Integer
Get
Return Math.Min(Math.Max(Me.currentBmp.Width, 80), 900)
End Get
End Property
Public ReadOnly Property ImageHeight() As Integer
Get
Return Math.Min(Math.Max(Me.currentBmp.Height, 80), 900)
End Get
End Property
End Class |
Partager