(v0.9) Drawing on the MapImage and Refresh()

Feb 17, 2008 at 4:57 AM
Hi,
I'm working with SharpMap 0.9 in VB.Net 2008. I'm just learning .Net and becoming familiar with SharpMap.
I'm attempting to duplicate a project I had created using ESRI's MapObjects LT2 and VB6.
One of the features is a "measure" tool, which requires drawing a line on the map with one point that is
fixed, and the the other that follows the mouse around.
I've been able to do that with SharpMap using the MouseDown and MouseMove events (to initialize and
track the line), and the Paint event to actually draw the line:

Private Sub mymap_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles mymap.Paint
' Show the measuring line.
' We must be using the measure tool and there must be a starting point
' gPointA and gPointB are form-level system.drawing.point objects that are initialized
' by the mousedown event and updated by the mousemove event.
If CurrentTool = "measuredistance" And (gPointA.X > 0 Or gPointA.Y > 0) Then
e.Graphics.DrawLine(Pens.Black, gPointA.X, gPointA.Y, gPointB.X, gPointB.Y)
End If
End Sub

This works, except that I have to call Refresh() on the MapImage to make it show up.

My questions:
1. is there a better way to draw on the map than what I'm doing?
2. Does Refresh() force the map to reload all the data in the map if there has been no change to the map itself?
If the answer to 2 is "yes", then my method won't work well when there is a lot of data in the map.

Thanks for any feedback.
Coordinator
Feb 17, 2008 at 6:56 AM
Hi robertvw -

This is pretty much the only way you can draw on the map in v0.9 - a call to GDI's DrawLine.

You don't necessarily have to re-draw the whole map, however.

Currently, calling Refresh() is re-rendering everything, but you could override this behavior. If you keep around a Bitmap of the map image (say, by just copying the control's graphics surface after a render cycle), you can blit this to the map control later via its Graphics.DrawImageUnscaledAndClipped() method. Then you can draw your line on top of it. Just remember to either save your whole line (as a GraphicsPath) or save the modified bitmap, since GDI forgets all about there ever being a line after you leave. Ah, the joys of non-retained mode graphics.