Centering a symbol on its point

May 24, 2012 at 4:59 PM


I have a VectorLayer containing point geometries that renders fine when I use a simple colour to represent each point. However, if I change the style to use a symbol then the symbol is drawn with its top left corner at the point instead of centred on it.

Is there a way to get the symbol to be centered on the point?

I've tried using SymbolOffset with arbitrary values and this works but I dont know how I can compute these values rather than hard code them.



May 28, 2012 at 1:03 PM

Anybody there?

May 29, 2012 at 2:16 PM

set Offset 1/2 width and 1/2 height of symbol ( bitmap)


PointF pfTemp = new PointF(point.X - bitMap.Width / 2, point.Y - bitMap.Height / 2);
e.Graphics.DrawImage(bitMap, pfTemp);

May 30, 2012 at 9:46 AM

Hi Trieuvy,

It would appear that the approach you mention is already applied during the render. This can be seen in the DrawPoint method in SharpMap.Rendering.VectorRenderer (the one that takes an Image as the third parameter).

However, i'm not convinced the image is being drawn correctly.

Basically i'm drawing a circle in one layer and the points in another. The origin of the circle is the same location as the points in the other layer so the point should always remain at the centre of the circle. If I dont specify a symbol for the point geometries then a simple colour is used. If I then display the layers then perform progressive zoom outs the point does remain at the centre of the circle. However, if I change the point symbol to be an image then perform the same progressive zoom out then the symbol moves from the centre of the circle to the bottom right as I zoom out.

Any ideas?

Jun 19, 2012 at 11:30 AM

I found the source of the problem with this.

In VectorRenderer DrawPoint(Graphics g, IPoint point, Image symbol, flat symbolscale, PointF offset, float rotation, Map map)

The call to DrawImageUnscaled uses an int for the x and y position of the image. This introduces an error because the image position is being moved by half the width and height of the image so that it is centered on the point.

Also, if a symbol scale is used then the DrawImage method is being called instead of DrawImageUnscaled, which uses float values instead. However, the x and y position is being cast to an int so the same problem occurs.


Is there really a need to check if the symbolscale is 1f and call DrawSymbolUnscaled? If the symbolscale is 1f then the code in the else statement wont change the scale. So simply removing the calls to DrawImageUnscaled and the surrounding if/else and calling DrawImage would suffice.

Remove the cast to int on the x and y positions in calls to DrawImage.

That fixes the problem for me. Are there any issues with getting this change added into SharpMap?


Jun 20, 2012 at 1:29 AM

Thanks harryscotter,

I will check this bug in sharpmap