Reduce size of image file returned

Topics: General Topics
Feb 26, 2007 at 10:52 PM
Can anyone suggest how to reduce the size of the image files being returned when using the URL provided by SharpMap.Web.Caching.InsertIntoCache(1, map.GetMap())? At present, a 600x400 pixel map image that is rendered (including a satellite image as the background) is about 700k. Can this be made smaller?

Feb 26, 2007 at 11:28 PM
Edited Feb 26, 2007 at 11:34 PM
You can encode it. I'd recommend PNG. The image returned from GetMap() is a 24 bit Bitmap (a GDI native bitmap). To encode, do a:
  using(System.IO.MemoryStream buffer = new System.IO.MemoryStream())
    map.GetMap().Save(buffer, System.Drawing.Imaging.ImageFormat.Png);
    Bitmap cachedImage = new Bitmap(buffer);
    SharpMap.Web.Caching.InsertIntoCache(1, cachedImage);
Feb 26, 2007 at 11:33 PM
Edited Feb 26, 2007 at 11:34 PM
...though really, we shouldn't be caching GDI objects on a web server...

Classes within the System.Drawing namespace are not supported for 
use within a Windows or ASP.NET service. Attempting to use these
classes from within one of these application types may produce
unexpected problems, such as diminished service performance and run-time exceptions.

You can probably get away with it, but MS discourages it. It's due to the inherent limitations of GDI as a non-client graphics library. Keep watching for 2.0 rendering to improve this, since WPF isn't limited in this way.