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

Does SharpMap support generating map image programmatically?

Topics: Algorithms, CompactFramework, Data Access, General Topics, SharpMap Project, SharpMap v0.9 / v1.x, SharpMap v2.0, Web Controls
Jan 26, 2011 at 3:57 PM
Edited Jan 26, 2011 at 5:19 PM

Does SharpMap support generating map image programmatically? If it does, could anyone please provide code or links for that.


Jan 26, 2011 at 5:15 PM

Hi, Im sorry, but what do you need ?


Do you want to render the image associated with the map? Map.GetMap() doesn't work ???


If so, I think you can take a look to the SharpMap.UI.Web.



Jan 26, 2011 at 5:21 PM

I want to find out how to use SharpMap to programmatically generate image (e.g. .png) associated with map.

Some sample code will be very much appreciated!

Jan 26, 2011 at 7:40 PM



Using a Windows Forms control you can draw a sample map?

Have you tried the Demo in the source code?

If you answer yes to both question, the only thing you must do to get a PNG is to call Map.GetMap() and this will return an Image Object that you can save as PNG.


Jan 26, 2011 at 8:10 PM
Edited Jan 26, 2011 at 8:11 PM

You said:

Have you tried the Demo in the source code?


Which version of ShaprMap, and what is the name of the file?


Jan 26, 2011 at 8:22 PM

Using the Trunk Version of SharpMap v0.9, you can try the DemoWinForm.


Jan 27, 2011 at 12:17 PM

The ExampleCodeSnipplets project uses this technique, too.

Hth FObermaier

Jan 27, 2011 at 1:07 PM
Edited Jan 27, 2011 at 2:16 PM

Which class object gets populated with spatial data?  What method plots shapes?



I downloaded SharpMap 0.9, when I run DemoWinForms, it throws exception:

  Message=Could not load file or assembly 'SharpMap, Version=0.9.4044.20775, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
  FileName=SharpMap, Version=0.9.4044.20775, Culture=neutral, PublicKeyToken=null


I changed all project to .net 4, otherwise it said verion issue.


I am using a previous version, Thus, the 2nd question is not important if you dont know. 

Jan 27, 2011 at 1:35 PM
Edited Jan 27, 2011 at 1:49 PM
Is there a code example that shows plotting a map of a country?
Jan 27, 2011 at 2:16 PM

you can also checkout this branch

there's a wms demo that maybe can fit into your needs. otherwise please specify what do you need.

Jan 27, 2011 at 3:27 PM
Edited Jan 27, 2011 at 3:28 PM

For SharpMap v0.9 have a look at this.

If you just want to get the image, all you need to do is change the return type to Image and change the return statement to

return map.GetMap();

You can set the Size of the image by passing a System.Drawing.Size object to the constructor of the map.

You can use any of the SharpMap providers to build your map. The sample just uses PostGis.

Hth FObermaier

Jan 27, 2011 at 5:25 PM


Thank you for your information. It is almost what I need, except SharMap only deal with Geometry type, file in SharpMap-78963\Trunk\SharpMap\Data\Providers\SqlServer2008.cs, while I have geography type. Thus, I modify the source code a bit.


Anyway, thank you again.

Jan 28, 2011 at 5:22 PM
Edited Jan 31, 2011 at 6:19 PM

The map generated is stretched a bit horizontally.

Any idea how to fix it?

Simpilfied code used below:

SharpMap.Map map = new SharpMap.Map(new System.Drawing.Size(500, 340));
SharpMap.Layers.VectorLayer layer = new SharpMap.Layers.VectorLayer("CountryLayer");

//initialise data source

//set map properties

map.BackColor = System.Drawing.Color.White;
System.Drawing.Image mapImage = map.GetMap();

Jan 29, 2011 at 8:43 AM

I'm not sure, but geography data type is lat/long, isn't it. If you draw a map without applying transformation to some projected coordinate system, your maps will look skewed.

See the Documentation | HowTo section for information on coordinate transformation

Hth FObermaier

Jan 29, 2011 at 11:22 AM
Edited Jan 29, 2011 at 12:23 PM

FObermaier Said: 'geography data type is lat/long, isn't it. If you draw a map without applying transformation to some projected coordinate system, your maps will look skewed.'

My data has been transformed to sharp map Geometry data type before plotting image. Do I miss something to correct the shape?

Jan 29, 2011 at 8:47 PM

If you have lat/long as coordinates and put that on a rectangular map, the map gets more and more distorted, the further you are away from the equator.

If you cannot life with that you need to apply coordinate transformation of some kind. Which one depends on your data.

Hth FObermaier

Jan 29, 2011 at 9:12 PM
The data used is SharpMap Geometry data type. What is the default coordinate system in SharpMap? Anothe issue is that some of the Map properties dont seem to work. However, it is not as important at this stage.
Jan 31, 2011 at 8:53 AM

When you draw geometries that have lat/long coordinates on a rectangular map, you will get some sort of distortion.

To get rid of this, you need to reproject your geometries to ProjectedCoordinateSystem (of your choice).

See Documentation | HowTo | Apply on the fly transformation ... for more information

Hth FObermaier

Jan 31, 2011 at 5:11 PM
Edited Feb 1, 2011 at 10:56 AM

I looked at the SharpMap-78963\Trunk\WinFormSamples\Samples\TileLayerSample.cs, I got a runtime error on parsing the second WKT (epsg3785), below is the code I try to run:


var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
var cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]");
var epsg3785 = cf.CreateFromWkt("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\",\"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\",\"6055\"]],PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\",\"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\",\"3785\"]]");
var ct = ctf.CreateFromCoordinateSystems(epsg4326, epsg3785);

I think there is a bug in SharpMap on parsing WKT. Is there a new version of ShaprMap dll that I can try?

OR instead of using CreateFromWkt, is there another way to create the same object?????
Jan 31, 2011 at 6:20 PM

I found the solution, as follows:


public static ICoordinateTransformation Transform2Mercator(ICoordinateSystem source)
            CoordinateSystemFactory cFac = new CoordinateSystemFactory();

            List<ProjectionParameter> parameters = new List<ProjectionParameter>();
            parameters.Add(new ProjectionParameter("latitude_of_origin", 0));
            parameters.Add(new ProjectionParameter("central_meridian", 0));
            parameters.Add(new ProjectionParameter("false_easting", 0));
            parameters.Add(new ProjectionParameter("false_northing", 0));
            IProjection projection = cFac.CreateProjection("Mercator""Mercator_2SP", parameters);

            IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Mercator",
                                                                                       source as IGeographicCoordinateSystem,
                                                                                       projection, LinearUnit.Metre,
                                                                                       new AxisInfo("East",
                                                                                       new AxisInfo("North",

            return new CoordinateTransformationFactory().CreateFromCoordinateSystems(source, coordsys);
Feb 1, 2011 at 11:14 AM
I have to create many VectoryLayers, and apply the same transformation to all the VectoryLayer instances, and add them to a map. 
Is there a better way to apply the transformation to the map rather than repeat the same transformation to the map's VectorLayer instance.

Code below

SharpMap.Map map = new SharpMap.Map(new System.Drawing.Size(500, 340));
SharpMap.Layers.VectorLayer layer = new SharpMap.Layers.VectorLayer("CountryLayer"); //many VectorLayers are created, omitted here

//initialise data source for layer

layer.DataSource = SetupDataSouce();

//apply transformation
var transform = Transform2Mercator();

//Apply transformation to a vectorlayer

layCountries.CoordinateTransformation = transform;

System.Drawing.Image mapImage = map.GetMap();

Feb 1, 2011 at 1:09 PM

You can use the same coordinate transformation object for all the layers, if they are all the same.

As a general rule of thumb, coordinate transformation is an expensive task, and if possible, it should be avoided by preprocessing the data with tools like ogr2ogr that are capable of reprojecting to the desired spatial reference system.

Hth FObermaier