This project has moved and is read-only. For the latest updates, please go here.

Highway Shields as Label Backgrounds?

Topics: SharpMap Project
Aug 16, 2008 at 2:15 AM
Edited Aug 16, 2008 at 2:16 AM

I've recently taken over development of our SharpMap C# application and was wondering if there is a way to use an icon or graphic as a background for a label? 

For example, use a US Interstate shield as the background for all labels identifying interstates for an interstate layer.

Aug 17, 2008 at 10:15 PM

Yes, I have done something similiar to this in v0.9 using a custom theme and GDI's DrawString method.

Assign a custom theme then load the interstate from the FeatureDataSet.  Next load your shield as a bitmap and draw the interstate number on the bitmap and then assign the new bitmap as the symbol to the returned style.

I have actually been considering doing this for Interstate shields.  For performance sake I might opt to actually have a predefined shield for each route.  You could even programattically generate your shield bitmaps from your data and store them as static images.  Then you would just load the appropriate bitmap in your custom theme instead of drawing the route number dynamically.  The dynamic method is possible though, I am doing it already with things that are more dynamic than route numbers.

Aug 25, 2008 at 4:46 PM
Sorry, it taken me awhile to respond.  Thanks for your response, but I was wondering if you could eleaborate a bit.  I haven't worked with setting up a theme or ther drawstring method.

Could you walk through your solution with some sample code?
Aug 25, 2008 at 6:26 PM
I was planning on using a LabelLayer to implement, but I'm unsure on how to set the symbol since this property only appears to be available on Vector layers?

Aug 26, 2008 at 2:58 PM
You will need to use a VectorLayer.  You will also need a seperate datasource with point locations for the shields.  I will try to get you some sample code for the CustomTheme and DrawString methods this week.
Oct 15, 2008 at 4:20 PM

Hi jrspence,

Sorry it has been so long.  I have been away from SharpMap for a while.  Here is some sample code for a bitmap helper to draw a string on an existing bitmap. 







using System.Collections.Generic;



using System.Linq;



using System.Web;



using System.Drawing;



/// <summary>




Summary description for BitMapHelper




/// </summary>




public class BitMapHelper







public static Bitmap GetShield(string routeNumber)



Bitmap shieldLabel =

new Bitmap(32, 32);


shieldLabel.SetResolution(96f, 96f);

Graphics g = Graphics.FromImage(shieldLabel);


//Load the shield from a resource or modify this to load from the file system




Bitmap shieldBitmap = Resources.Bitmaps.Shield;



//first draw the bitmap onto our label bitmap starting at upper left cornder (0,0(




g.DrawImage(shieldBitmap, 0, 0);


Font textFont =

new Font(FontFamily.GenericMonospace, 12);



//next draw the string on top




g.DrawString(routeNumber, textFont, Brushes.Black, new PointF(0,0));



//clean up








return shieldLabel;




Oct 15, 2008 at 4:25 PM

Here is the layer setup with a custom theme.

SharpMap.Layers.VectorLayer layShields =

new SharpMap.Layers.VectorLayer("Cities");



//Set the datasource to a shapefile in the App_data folder



//this will have to the points where you want the shields to be located


layShields.DataSource =

new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\shields.shp"), true);



//Define a blue 1px wide pen



//layShields.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\App_data\icon.png"));




//define styles and custom theme


SharpMap.Styles.VectorStyle shieldStyle =

new SharpMap.Styles.VectorStyle();


shieldStyle.SymbolScale = 0.8f;

shieldStyle.Symbol = Resources.Bitmaps.Shield;


//create a custom theme


SharpMap.Rendering.Thematics.CustomTheme shieldTheme =

new SharpMap.Rendering.Thematics.CustomTheme(



delegate(FeatureDataRow dr)




//get the population



string population = dr["ROUTENUMBER"].ToString();


SharpMap.Styles.VectorStyle style =

new SharpMap.Styles.VectorStyle();


style =

new SharpMap.Styles.VectorStyle();


style.SymbolScale = 0.8f;


//use the bitmap helper to draw a new bitmap


style.Symbol = BitMapHelper.GetShield(routeNumber);


return style;



shieldTheme.DefaultStyle = shieldStyle;

layShields.Theme = shieldTheme;

layShields.MaxVisible = 40;

layShields.SRID = 4326;