This project has moved. For the latest updates, please go here.

drawing selected area

Topics: SharpMap v2.0, WinForms Controls
Sep 2, 2008 at 12:46 PM

I'm trying to create a custom MapTool (v2.0). That tool is some sort of a selection tool and while the user drag the mouse over the map I want to draw a semi transperent rectangle over the selected area.

I used the Selection object (ViewSelection2D) on the MapView (MapViewControl) but since the result of every rendering is cached to the next rendering the transperncy in my selection area isn't constant and if I rag the mouse back (to shrink the selection area) the area that was previously selected is still colored.

I can see one way to solve this problam, my idea is to extend the GdiRenderObject to hold information whether to cache this object, and in the rendering process (the OnPaint method in the MapViewControl) to draw those non-cached object directly to the screen (screenGraphics) and not to the cached-image (g, that write over the _bufferedMapImage).

But I want to check if there is a less drastic solution to that problam.
Sep 2, 2008 at 4:42 PM
Edited Sep 2, 2008 at 5:37 PM
If you want to use the selection-object, I think it is sufficient to prohibit switching the graphics context to the _bufferedMapImage when _presenter.IsRenderingSelection is true.
Like this:
protected override void OnPaint(PaintEventArgs e)
            if (_bufferedMapImage == null)
                _bufferedMapImage = new Bitmap(Width, Height);

            if (!_presenter.IsRenderingSelection)
              g = Graphics.FromImage(_bufferedMapImage);

            g.SmoothingMode = SmoothingMode.AntiAlias;

            g.Transform = getGdiViewTransform();

            while (_renderObjectQueue.Count > 0)
This has the drawback, that once the selection is rendered to the screen, it isn't rendered anymore after another window was on top of the mapimage, though.

Sep 3, 2008 at 8:11 AM
Edited Sep 3, 2008 at 8:16 AM
That work fine, thank you.

The fact that the selection area is not re-rendered is not a problam, the selection area need to be deleted on lost of focus anyway.

But the fact I need to change the MapViewControl (or create a copy of it) is not such a great solution (this cannot be handle with inheritance since the _presenter is private on the MapViewControl, and the MapPresenter is internal to the SharpMap.Presentation.WinForm assembly).

Another solution I have that does not involve changing the base MapViewControl is to wrap it in a new control that will hold a PictureBox above the MapViewControl and all the non-cached display object will be drawn on that PictureBox, but i'm not so sure it will be as simple as I hope.
Sep 3, 2008 at 12:31 PM
Another thing that disturb me is that rendering accour after each point addition to the SelectionView2D, I want to enter all my points (4 of them) before there is any rendering.

The best solution to this was to enable insertion of point array or a path, or make the the SelectRectangle work (the Expand method on the generic ViewSelection class is not implemented).