Thinking in GIS

a blog about GIS from a urban geogeek living at the countryside

Feed, Categories, Archives


Do you get errors in the mapscript c# tutorial?...

Posted: February 29, 2008
Categories: GIS, Windows, .NET, PostGIS, devs, MapServer, Tutorials
Feedback: View Comments

...well, this is the solution to your problems (at least I hope so).

I am continuing receiving emails from people in despair that cannot successfully use this tutorial. I am sorry but I cannot answer to all this emails, so I thought to write this post in order to allow people to configure it correctly.

BTW, this demo is still working since 2 years without any problems, and the code there is just the same you can download from this blog. So it must work also for you ;-)

So if you just cannot use it, this is a check list:

If you receive the "Unable to load dll (mapscript)" error, look at this article from Tamas Szekeres (the mapscript c# mantainer).

If you cannot compile the solution, be sure to have installed MapServer and the needed dlls. You need to set the path to them, for example in my windows box in my PATH environment variable there is: c:ms4wApachecgi-binmapscript and c:ms4wApachecgi-binmapscriptcsharp. If you are still receiving compile errors consider to copy all the dlls in the bin solution's folder (as in the source code you can download here). You need to put in the PATH or copy in the bin folder the dlls of the MapServer version you need.

And don't forget to take a look to this important article still from Tamas.

Error in c# MapScript Tutorial

If you can compile the solution, but you receive a blank image as this one:

  • in the mapfile check that the path to layers is the one where YOU effectively have your shapefiles, PostGIS layer, etc...
  • still in the mapfile check if you have the needed fonts. If you are not sure about that, just try the mapfile without using the LABEL sections. After it is working concentrate on fonts
  • note that the mapscript errors are not intercepted by default from Visual Studio, so the code will continue to execute even if MapServer throws errors. A good way to understand the source of the error is making some error handling strategy - and you should always do, like for example in this method (the one that effectively generates the map):
/// <summary>
/// Refresh MapServer map and send the image stream to output
/// </summary>
private void RefreshMap()
{
    try
    {
        using(imageObj image = map.draw())
        {
            byte[] img = image.getBytes();
            using (MemoryStream ms = new MemoryStream(img))
            {
                System.Drawing.Image mapimage = System.Drawing.Image.FromStream(ms); 
                Bitmap bitmap = (Bitmap)mapimage;
                bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }                
        }
    }
    catch (Exception e)
    {
        System.Diagnostics.Debug.WriteLine(e.Message);
    }
}

Hope it will work for you now! ;-)

blog comments powered by Disqus