Drawing on map canvas

Topics: General Topics
Apr 13, 2007 at 9:25 AM
I need to draw something directly on the map canvas BEFORE any other layers are drawn.
Actually its just a statuc bitmap I need to use as canvas, is that possible and how?
Apr 13, 2007 at 10:58 AM
This may give you a clue

System.Drawing.Bitmap img = (System.Drawing.Bitmap)map.GetMap();
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
g.DrawString("for example", new Font("Arial", 9), new System.Drawing.SolidBrush(Color.Black), new PointF(10,10));
Apr 13, 2007 at 11:38 AM
Thank you.

Well the broblem is not the use of GDI but when to access the maps "graphics" object before anything else is drawn..

For now I've solved it by creating a new layer-type and put it at the bottom of the stable.
Apr 17, 2007 at 4:29 AM
Hi
For now I've solved it by creating a new layer-type and put it at the bottom of the stable.

How to??
Apr 26, 2007 at 6:56 AM
I simply created a new layer and inherited it from SharpMap.Layers.Layer

The following example draws a simple ruler in the bottom-left cornor. Place it anywhere in the layer-list put it behind other layers.



public class RulerLayer : SharpMap.Layers.Layer, IDisposable
{
public RulerLayer()
{

}

public override void Render(Graphics g, SharpMap.Map map)
{
Font defaultFont = new Font("Arial", 12f);

double mapWidth = map.Zoom / 2;

double toNearest = 0;
if (mapWidth > 1000)
toNearest = 1000;
else
toNearest = 100;

double rest = mapWidth % toNearest;
double nearest = 0;
//check if rest is > toNearest/2
if (rest > (toNearest / 2))
nearest = (mapWidth - rest) + toNearest;
else
nearest = (mapWidth - rest);

int lengthInPixels = (int)(nearest / map.PixelSize);
System.Drawing.Drawing2D.SmoothingMode oSmoothingMode = g.SmoothingMode;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
g.FillRectangle(Brushes.White, 20, (int)(map.Size.Height - 30), lengthInPixels, 10);
g.DrawRectangle(Pens.Black, 20, (int)(map.Size.Height - 30), lengthInPixels, 10);
g.FillRectangle(Brushes.Black, 20, (int)(map.Size.Height - 25), lengthInPixels / 4, 5);
g.FillRectangle(Brushes.Black, 20 + (lengthInPixels / 4), (int)(map.Size.Height - 30), lengthInPixels / 4, 5);
g.DrawLine(Pens.Black, 20 + (lengthInPixels / 2), (int)(map.Size.Height - 30), 20 + (lengthInPixels / 2), (int)(map.Size.Height - 20));

g.SmoothingMode = oSmoothingMode;

double nearestval = 0;
if (nearest > 1000)
nearestval = nearest / 1000;
else
nearestval = nearest;

System.Drawing.Drawing2D.GraphicsPath pth = new System.Drawing.Drawing2D.GraphicsPath();

SizeF textSize = g.MeasureString("0", defaultFont);
pth.AddString("0", defaultFont.FontFamily, (int)FontStyle.Bold, defaultFont.SizeInPoints, new PointF(20 - (int)(textSize.Width / 2), (int)(map.Size.Height - 30 - textSize.Height)), StringFormat.GenericDefault);
g.FillPath(Brushes.White, pth);
g.DrawPath(Pens.Black, pth);

string dispString = string.Format("{0}", nearestval / 4);
textSize = g.MeasureString(dispString, defaultFont);
pth.AddString(dispString, defaultFont.FontFamily, (int)FontStyle.Bold, defaultFont.SizeInPoints, new PointF(20 - (int)(textSize.Width / 2) + (lengthInPixels / 4), (int)(map.Size.Height - 30 - textSize.Height)), StringFormat.GenericDefault);
g.FillPath(Brushes.White, pth);
g.DrawPath(Pens.Black, pth);

dispString = string.Format("{0}", nearestval / 2);
textSize = g.MeasureString(dispString, defaultFont);
pth.AddString(dispString, defaultFont.FontFamily, (int)FontStyle.Bold, defaultFont.SizeInPoints, new PointF(20 - (int)(textSize.Width / 2) + (lengthInPixels / 2), (int)(map.Size.Height - 30 - textSize.Height)), StringFormat.GenericDefault);
g.DrawPath(Pens.Black, pth);
g.FillPath(Brushes.White, pth);

dispString = string.Format("{0}", nearestval);
textSize = g.MeasureString(dispString, defaultFont);
pth.AddString(dispString, defaultFont.FontFamily, (int)FontStyle.Bold, defaultFont.SizeInPoints, new PointF(20 - (int)(textSize.Width / 2) + lengthInPixels, (int)(map.Size.Height - 30 - textSize.Height)), StringFormat.GenericDefault);
g.DrawPath(Pens.Black, pth);
g.FillPath(Brushes.White, pth);

if (nearest > 1000)
dispString = "kilometer"; // g.DrawString("kilometer", new Font("Arial", 12), Brushes.White, lengthInPixels + 25, (int)(map.Size.Height - 30));
else
dispString = "meter"; //g.DrawString("meter", new Font("Arial", 12), Brushes.White, lengthInPixels + 25, (int)(map.Size.Height - 30));
pth.AddString(dispString, defaultFont.FontFamily, (int)FontStyle.Bold, defaultFont.SizeInPoints, new PointF(lengthInPixels + 25, (int)(map.Size.Height - 30)), StringFormat.GenericDefault);
g.DrawPath(Pens.Black, pth);
g.FillPath(Brushes.White, pth);

}

public override BoundingBox Envelope
{
get
{
return null;
}
}

#region ICloneable Members

/// <summary>
/// Clones the layer
/// </summary>
/// <returns>cloned object</returns>
public override object Clone()
{
throw new Exception("Clone not implemented.");
}

#endregion

#region IDisposable Members

/// <summary>
/// Disposes the object
/// </summary>
public void Dispose()
{

}

#endregion

}