using System; using System.Collections.ObjectModel; using System.Drawing.Drawing2D; using System.IO; using System.Linq; using ThinkGeo.MapSuite.Core; namespace CSHowDoISamples.Getting_Started { public class RadialGradientAreaStyle : AreaStyle { private Collection gradientStops; private double resolution; private System.Drawing.Color startColor; public RadialGradientAreaStyle() : this(new PointShape(), 1, 1) { } public RadialGradientAreaStyle(PointShape center, double radiusX, double radiusY) { Center = center; RadiusX = radiusX; RadiusY = radiusY; gradientStops = new Collection(); } public PointShape Center { get; set; } public double RadiusX { get; set; } public double RadiusY { get; set; } public Collection GradientStops { get { return gradientStops; } } protected override void DrawCore(System.Collections.Generic.IEnumerable features, GeoCanvas canvas, Collection labelsInThisLayer, Collection labelsInAllLayers) { base.DrawCore(features, canvas, labelsInThisLayer, labelsInAllLayers); resolution = Math.Max(canvas.CurrentWorldExtent.Width / canvas.Width, canvas.CurrentWorldExtent.Height / canvas.Height); GeoGradientStop startStop = GradientStops.FirstOrDefault((stop) => { return stop.Offset == 0; }); if (startStop != null) { startColor = ToColor(startStop.Color); } foreach (Feature feature in features) { if (feature.GetWellKnownType() == WellKnownType.Polygon) { DrawGraphicsPathFromFeature(feature, canvas); } } } private void DrawGraphicsPathFromFeature(Feature feature, GeoCanvas geoCanvas) { RectangleShape rectangleShape = feature.GetBoundingBox(); PointShape centerWorldPoint = rectangleShape.GetCenterPoint(); double centerScreenX = (centerWorldPoint.X - geoCanvas.CurrentWorldExtent.UpperLeftPoint.X) / resolution; double centerScreenY = (geoCanvas.CurrentWorldExtent.UpperLeftPoint.Y - centerWorldPoint.Y) / resolution; float featureScreenWidth = (float)(rectangleShape.Width / resolution); float featureScreenHeight = (float)(rectangleShape.Height / resolution); GraphicsPath graphicPath = new GraphicsPath(); graphicPath.AddEllipse((1 - (float)RadiusX) * featureScreenWidth * .5f, (1 - (float)RadiusY) * featureScreenHeight * .5f, featureScreenWidth * (float)RadiusX, featureScreenHeight * (float)RadiusY); PathGradientBrush brush = new PathGradientBrush(graphicPath); brush.CenterPoint = new System.Drawing.PointF(featureScreenWidth * (float)Center.X, featureScreenHeight * (float)Center.Y); brush.CenterColor = startColor; System.Drawing.Color[] blendColors = new System.Drawing.Color[GradientStops.Count]; float[] blendOffset = new float[GradientStops.Count]; int stopIndex = 0; foreach (GeoGradientStop stop in GradientStops) { blendColors[stopIndex] = ToColor(stop.Color); blendOffset[stopIndex] = (float)stop.Offset; stopIndex++; } ColorBlend blend = new ColorBlend(); blend.Colors = blendColors; blend.Positions = blendOffset; brush.InterpolationColors = blend; PolygonShape polygonShape = feature.GetShape() as PolygonShape; if (polygonShape != null) { System.Drawing.Point[] polygonPoints = new System.Drawing.Point[polygonShape.OuterRing.Vertices.Count]; if (polygonShape.InnerRings.Count == 0) { for (int i = 0; i < polygonShape.OuterRing.Vertices.Count; i++) { Vertex currentPoint = polygonShape.OuterRing.Vertices[i]; int currentScreenX = (int)((currentPoint.X - rectangleShape.UpperLeftPoint.X) / resolution); int currentScreenY = (int)((rectangleShape.UpperLeftPoint.Y - currentPoint.Y) / resolution); polygonPoints[i] = new System.Drawing.Point(currentScreenX, currentScreenY); } System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap((int)featureScreenWidth, (int)featureScreenHeight); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); Stream streamSource = new MemoryStream(); try { graphics.FillPolygon(brush, polygonPoints); graphics.Flush(); bitmap.Save(streamSource, System.Drawing.Imaging.ImageFormat.Png); GeoImage geoImage = new GeoImage(streamSource); geoCanvas.DrawScreenImageWithoutScaling(geoImage, (float)centerScreenX, (float)centerScreenY, DrawingLevel.LevelOne, 0f, 0f, 0f); } finally { if (bitmap != null) { bitmap.Dispose(); } if (graphics != null) { graphics.Dispose(); } } } } } private System.Drawing.Color ToColor(GeoColor geoColore) { return System.Drawing.Color.FromArgb(geoColore.AlphaComponent, geoColore.RedComponent, geoColore.GreenComponent, geoColore.BlueComponent); } } public class GeoGradientStop { public GeoGradientStop() : this(GeoColor.StandardColors.Transparent, 1) { } public GeoGradientStop(GeoColor color, double offset) { Color = color; Offset = offset; } public GeoColor Color { get; set; } public double Offset { get; set; } } }