Common_CustomRenderers_CSharp\App_Code\SimplePointRenderer.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> /// Renders point graphic features with the image specified by the features' attribute data /// </summary> [System.Serializable] public class SimplePointRenderer : ESRI.ADF.Samples.Renderers.RendererBase { private string imagePathColumn = "ImagePath"; /// <summary> /// Name of the column containing the path to the image that will be used to symbolize the feature /// </summary> public string ImagePathColumn { get { return imagePathColumn; } set { imagePathColumn = value; } } #region IRenderer Members /// <summary> /// Main part of the IRenderer interface, within which a feature encapsulating the specified DataRow is to be /// rendered on the specified graphics surface. The geometry instance has already been transformed to screen /// coordinate, so we don't have to worry about that here. /// </summary> /// <param name="row">row containing the feature's data</param> /// <param name="graphics">GDI+ surface on which to render the feature</param> /// <param name="geometryColumn">column containing the feature's geometry</param> public override void Render(System.Data.DataRow row, System.Drawing.Graphics graphics, System.Data.DataColumn geometryColumn) { // Validate method input if (row == null || graphics == null || geometryColumn == null) return; // Validate input geometry. This renderer only supports points. ESRI.ArcGIS.ADF.Web.Geometry.Geometry geometry = row[geometryColumn] as ESRI.ArcGIS.ADF.Web.Geometry.Geometry; if (geometry == null || !(geometry is ESRI.ArcGIS.ADF.Web.Geometry.Point)) return; // Get the input point ESRI.ArcGIS.ADF.Web.Geometry.Point p = geometry as ESRI.ArcGIS.ADF.Web.Geometry.Point; // Make sure the feature contains the image path column specified on the renderer if (row.Table.Columns.Contains(this.ImagePathColumn)) { // Get the path to the image string imagePath = row[this.ImagePathColumn].ToString(); // Convert the relative path to its absolute equivalent imagePath = System.Web.HttpContext.Current.Server.MapPath(imagePath); // Make sure the image exists if (!System.IO.File.Exists(imagePath)) { System.Diagnostics.Debug.Fail("Image path '" + imagePath + "' not found"); return; } // Get the image and draw it at the location specified by the input point using (System.Drawing.Image image = System.Drawing.Image.FromFile(imagePath)) { graphics.DrawImageUnscaled(image, System.Convert.ToInt32(p.X), System.Convert.ToInt32(p.Y)); } } } /// <summary> /// Creates swatches used for the Table of Contents / Legend. /// This is automatically called by IMapTocFunctionality when generating the TOC. /// </summary> /// <param name="swatchInfo"></param> /// <param name="fileName"></param> /// <param name="minScale"></param> /// <param name="maxScale"></param> /// <returns></returns> public override ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchCollection GenerateSwatches( ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchInfo swatchInfo, string fileName, string minScale, string maxScale) { ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchCollection swatches = new ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchCollection(); ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchUtility swatchUtility = new ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchUtility(swatchInfo); // Create the swatches and add them to the swatch collection. The code assumes three images placed in a // website folder named "images" and named 1.gif, 2.gif, and 3.gif. for(int i = 1;i <= 3;i++) { // Get the absolute path to the current image string swatchPath = string.Format("~/images/{0}.gif", i); swatchPath = System.Web.HttpContext.Current.Server.MapPath(swatchPath); // Create a symbol from the image ESRI.ArcGIS.ADF.Web.Display.Symbol.RasterMarkerSymbol swatchSymbol = new ESRI.ArcGIS.ADF.Web.Display.Symbol.RasterMarkerSymbol(swatchPath); // Generate the swatch image and add it to the collection ESRI.ArcGIS.ADF.Web.CartoImage swatchImage = swatchUtility.DrawNewSwatch(swatchSymbol, null); swatches.Add(new ESRI.ArcGIS.ADF.Web.Display.Swatch.Swatch(swatchImage, "Marker #" + i.ToString(), null, null)); } return swatches; } #endregion } }