Common_CustomRenderers_CSharp\App_Code\Utility.cs
// Copyright 2010 ESRI // // All rights reserved under the copyright laws of the United States // and applicable international laws, treaties, and conventions. // // You may freely redistribute and use this sample code, with or // without modification, provided you include the original copyright // notice and use restrictions. // // See the use restrictions. // namespace ESRI.ADF.Samples.Renderers { /// <summary> /// Class containing various helper methods for rendering features /// </summary> internal class Utility { /// <summary> /// Converts a transparency percentage to a 0-255 alpha value /// </summary> /// <param name="percentage">Percentage to convert</param> /// <returns>Corresponding alpha value, as a byte</returns> public static byte TransparencyToAlpha(double percentage) { double value = 0; value = 100 - percentage; value = value / 100; value = value * 255; if (value < 0) value = 0; else if (value > 255) value = 255; return (byte)value; } /// <summary> /// Converts a Web ADF polygon to a GDI+ 2D graphics path /// </summary> /// <param name="adfPolygon">Polygon to convert</param> /// <returns>The corresponding graphics path</returns> public static System.Drawing.Drawing2D.GraphicsPath PolygonToPath( ESRI.ArcGIS.ADF.Web.Geometry.Polygon adfPolygon) { return Utility.PolygonToPath(adfPolygon, 0, 0); } /// <summary> /// Converts a Web ADF polygon to a GDI+ 2D graphics path, offset by the specified amounts /// </summary> /// <param name="adfPolygon"></param> /// <param name="offsetX"></param> /// <param name="offsetY"></param> /// <returns>The corresponding graphics path</returns> public static System.Drawing.Drawing2D.GraphicsPath PolygonToPath( ESRI.ArcGIS.ADF.Web.Geometry.Polygon adfPolygon, int offsetX, int offsetY) { System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); // Loop through the polygon's rings, adding each to the graphics path foreach (ESRI.ArcGIS.ADF.Web.Geometry.Ring adfRing in adfPolygon.Rings) { // Add the current ring to the path graphicsPath.AddPolygon(pointCollectionToPointArray(adfRing.Points, offsetX, offsetY)); // Add each of the current ring's holes to the path foreach (ESRI.ArcGIS.ADF.Web.Geometry.Hole ring in adfRing.Holes) graphicsPath.AddPolygon(pointCollectionToPointArray(ring.Points, offsetX, offsetY)); } return graphicsPath; } /// <summary> /// Converts a Web ADF polyline to a GDI+ 2D graphics path /// </summary> /// <param name="polyline">The polyline to convert</param> /// <returns>The corresponding graphics path</returns> public static System.Drawing.Drawing2D.GraphicsPath PolylineToPath(ESRI.ArcGIS.ADF.Web.Geometry.Polyline adfPolyline) { System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); // Loop through the paths in the passed-in polyline, adding each to the graphics path foreach (ESRI.ArcGIS.ADF.Web.Geometry.Path adfPath in adfPolyline.Paths) { graphicsPath.AddLines(pointCollectionToPointArray(adfPath.Points, 0, 0)); } return graphicsPath; } /// <summary> /// Draws a GDI+ polygon on the specified surface corresponding to the specified Web ADF polygon. /// Applies a solid fill with the specified color and transparency and offsets the polygon by the /// specified amount. /// </summary> public static void FillPolygon(System.Drawing.Graphics graphics, ESRI.ArcGIS.ADF.Web.Geometry.Polygon adfPolygon, System.Drawing.Color color, int transparency, int offsetX, int offsetY) { if (adfPolygon == null) return; // Create a GDI+ graphics path from the passed-in polygon using (System.Drawing.Drawing2D.GraphicsPath graphicsPath = Utility.PolygonToPath(adfPolygon, offsetX, offsetY)) { // Calculate the fill color after the specified transparency is applied System.Drawing.Color fillColor = System.Drawing.Color.FromArgb(Utility.TransparencyToAlpha(transparency), color); // Draw the polygon on the surface using (System.Drawing.SolidBrush solidBrush = new System.Drawing.SolidBrush(fillColor)) { graphics.FillPath(solidBrush, graphicsPath); } } } /// <summary> /// Renders a Web ADF polygon on the specified GDI+ surface with the specified color and outline width /// </summary> public static void DrawPolygon(System.Drawing.Graphics graphics, ESRI.ArcGIS.ADF.Web.Geometry.Polygon adfPolygon, System.Drawing.Color outlineColor, float outlineWidth) { Utility.DrawPolygon(graphics, adfPolygon, outlineColor, outlineWidth, 0, 0); } /// <summary> /// Renders a Web ADF polygon on the specified GDI+ surface with the specified color, outline width, and offset /// </summary> public static void DrawPolygon(System.Drawing.Graphics graphics, ESRI.ArcGIS.ADF.Web.Geometry.Polygon adfPolygon, System.Drawing.Color outlineColor, float outlineWidth, int offsetX, int offsetY) { if (adfPolygon == null) return; // Create a GDI+ graphics path from the passed-in polygon using (System.Drawing.Drawing2D.GraphicsPath graphicsPath = Utility.PolygonToPath(adfPolygon, offsetX, offsetY)) { // Draw the polygon on the surface using (System.Drawing.Pen pen = new System.Drawing.Pen(outlineColor,outlineWidth)) { graphics.DrawPath(pen, graphicsPath); } } } /// <summary> /// Renders a Web ADF polyline on the specified GDI+ surface with the specified color and thickness /// </summary> public static void DrawPolyline(System.Drawing.Graphics graphics, ESRI.ArcGIS.ADF.Web.Geometry.Polyline adfPolyline, System.Drawing.Color color, float width) { if (adfPolyline == null) return; // Convert the polyline to a GDI+ graphics path using (System.Drawing.Drawing2D.GraphicsPath path = Utility.PolylineToPath(adfPolyline)) { // Draw the polyline on the GDI+ surface using (System.Drawing.Pen pen = new System.Drawing.Pen(color, width)) { graphics.DrawPath(pen, path); } } } // Converts a Web ADF point collection to a screen point array, applying any specified offset // to each point private static System.Drawing.Point[] pointCollectionToPointArray( ESRI.ArcGIS.ADF.Web.Geometry.PointCollection points, int offsetX, int offsetY) { // Loop through each point in the point collection, creating a corresponding screen // point in the point array for each System.Drawing.Point[] pointArray = new System.Drawing.Point[points.Count]; for (int i = 0; i < points.Count; i++) { pointArray[i] = new System.Drawing.Point( System.Convert.ToInt32(points[i].X - offsetX), System.Convert.ToInt32(points[i].Y - offsetY)); } return pointArray; } } }