// create Image for Legend Based on Layer and Current Scale private static Boolean CreateLegendImage(ref clsDatabaseTableItem oDatabaseTableItem, ref clsFeatureLayerRender oFeatureLayerRender) { string TheLogMessage; string TheLoggerMethodNameValue; int TheWidthInt; int TheHeightInt; double TheWidthDouble; double TheHeightDouble; float TheCenterXInScreen; float TheCenterYInScreen; System.Drawing.Bitmap TheBitmap; ThinkGeo.MapSuite.Drawing.GeoImage TheGeoImage; ThinkGeo.MapSuite.Drawing.PlatformGeoCanvas ThePlatformGeoCanvas; ShapeFileType msShapeFileType; RectangleShape TheRectangle; // initializations oFeatureLayerRender.Legend = null; ThePlatformGeoCanvas = new ThinkGeo.MapSuite.Drawing.PlatformGeoCanvas(); TheLoggerMethodNameValue = string.Format("{0}", "CreateLegendImage"); TheWidthInt = 32; TheHeightInt = 20; // create bitmap according to ShapeFileType try { TheBitmap = new System.Drawing.Bitmap(TheWidthInt, TheHeightInt); TheWidthDouble = TheBitmap.Width; TheHeightDouble = TheBitmap.Height; TheRectangle = new RectangleShape(0, TheHeightDouble, TheWidthDouble, 0); ThePlatformGeoCanvas.BeginDrawing(TheBitmap, TheRectangle, GeographyUnit.Feet); msShapeFileType = oDatabaseTableItem.ShapeFileType; switch (msShapeFileType) { case ShapeFileType.Point: case (ShapeFileType.Multipoint): oFeatureLayerRender.PointStyle.DrawSample(ThePlatformGeoCanvas); break; case ShapeFileType.Polygon: oFeatureLayerRender.AreaStyle.DrawSample(ThePlatformGeoCanvas); break; case ShapeFileType.Polyline: oFeatureLayerRender.LineStyle.DrawSample(ThePlatformGeoCanvas); break; default: TheLogMessage = String.Format("UnKnown ShapeFileType/WellKnownType of FeatureLayer({0}/{1}){2}DatabaseName={3}, LayerName={4}{5}Directory={6}", oDatabaseTableItem.ShapeFileType.ToString(), oDatabaseTableItem.WellKnownType.ToString(), Environment.NewLine, oFeatureLayerRender.DatabaseName, oFeatureLayerRender.LayerName, Environment.NewLine, oFeatureLayerRender.Directory); TheAssetsStatic.aTheUtlLog4Net.UtlLog4NetLogInfo(TheAssetsStatic.gsAppAssemblyName, TheLoggerClassNameValue, TheLoggerMethodNameValue, ref TheLogMessage, OriStarLogging.UtlLog4Net.LoggingLevel.Error, (OriStarLogging.UtlLog4Net.LoggingDestination.Historical | OriStarLogging.UtlLog4Net.LoggingDestination.ComeBack)); return (false); } TheGeoImage = ThePlatformGeoCanvas.ToGeoImage(TheBitmap); TheCenterXInScreen = (float)(TheWidthDouble / 2); TheCenterYInScreen = (float)(TheHeightDouble / 2); ThePlatformGeoCanvas.DrawScreenImageWithoutScaling(TheGeoImage, TheCenterXInScreen, TheCenterYInScreen, DrawingLevel.LevelOne, 0, 0, 0); ThePlatformGeoCanvas.EndDrawing(); ThePlatformGeoCanvas.Flush(); oFeatureLayerRender.Legend = ToWpfBitmap(TheBitmap); // dispose TheBitmap.Dispose(); } catch (Exception ex) { TheLogMessage = String.Format("Unable to Create a Legend Bitmap Image for ShapeFileType/WellKnownType of FeatureLayer({0}/{1}){2}DatabaseName={3}, LayerName={4}{5}Directory={6}", oDatabaseTableItem.ShapeFileType.ToString(), oDatabaseTableItem.WellKnownType.ToString(), Environment.NewLine, oFeatureLayerRender.DatabaseName, oFeatureLayerRender.LayerName, Environment.NewLine, oFeatureLayerRender.Directory); TheAssetsStatic.aTheUtlLog4Net.UtlLog4NetLogInfo(TheAssetsStatic.gsAppAssemblyName, TheLoggerClassNameValue, TheLoggerMethodNameValue, ref TheLogMessage, OriStarLogging.UtlLog4Net.LoggingLevel.Error, (OriStarLogging.UtlLog4Net.LoggingDestination.Historical | OriStarLogging.UtlLog4Net.LoggingDestination.ComeBack), ref ex); return (false); } return (true); } // convert Bitmap to Png public static BitmapSource ToWpfBitmap(System.Drawing.Bitmap TheBitmap) { BitmapImage TheBitmapImage; // initializations TheBitmapImage = new BitmapImage(); using (MemoryStream TheStream = new MemoryStream()) { TheBitmap.Save(TheStream, System.Drawing.Imaging.ImageFormat.Png); TheStream.Position = 0; TheBitmapImage.BeginInit(); // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed." // Force the bitmap to load right now so we can dispose the stream. TheBitmapImage.CacheOption = BitmapCacheOption.OnLoad; TheBitmapImage.StreamSource = TheStream; TheBitmapImage.EndInit(); TheBitmapImage.Freeze(); // dispose Stream to prevent MemoryLeak TheStream.Dispose(); return (TheBitmapImage); }