====== Source Code ServicesEditionSample ScalingImageStyle CS 090728.zip ======
====Program.cs====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace ScalingImageStyle
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TestForm());
}
}
}
====ScalingImageStyle.cs====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThinkGeo.MapSuite.Core;
namespace ScalingImageStyle
{
class ScalingImageStyle : Style
{
private GeoImage image = null;
private GeographyUnit mapUnit;
private double maximumScale;
private double minimumScale;
private double maximumSize;
private double minimumSize;
private float xOffsetInPixel;
private float yOffsetInPixel;
private float rotateAngle;
// A default constructor that just calls the main constructor
public ScalingImageStyle()
: this(GeographyUnit.DecimalDegree,null,double.MaxValue, 0,20,10 )
{
}
// A constructor with fewer parameters that calls the main constructor
public ScalingImageStyle(GeographyUnit mapUnit, GeoImage image, double MaximumScale, double MinimumScale, double MaximumSize, double MinimumSize)
{
this.image = image;
this.maximumScale = MaximumScale;
this.minimumScale = MinimumScale;
this.maximumSize = MaximumSize;
this.minimumSize = MinimumSize;
this.mapUnit = mapUnit;
}
// This is the map unit to determine the scale.
public GeographyUnit MapUnit
{
get { return mapUnit; }
set { mapUnit = value; }
}
// This is the image that will be scaled.
public GeoImage Image
{
get { return image; }
set { image = value; }
}
// The maximum scale will be the largest scale used to calculate the size of the image.
// If the scale gets larger than the maximum, then we compute the scaling based on
// this number instead. This means that after this scale, the image will not get
// any smaller no matter how much more you zoom out.
public double MaximumScale
{
get { return maximumScale; }
set { maximumScale = value; }
}
// The minimum scale will be the smallest scale used to calculate the size of the image.
// If the scale gets smaller than the minimum, then we compute the scaling based on
// this number instead. This means that after this scale, the image will not get
// any larger no matter how much more you zoom in.
public double MinimumScale
{
get { return minimumScale; }
set { minimumScale = value; }
}
// The MaximumSize is the size of the image at MinimumScale and lower.
public double MaximumSize
{
get { return maximumSize; }
set { maximumSize = value; }
}
// The MinimumSize is the size of the image at MaximumScale and higher.
public double MinimumSize
{
get { return minimumSize; }
set { minimumSize = value; }
}
// This is the X offset in pixels for the image.
public float XOffsetInPixel
{
get { return xOffsetInPixel; }
set { xOffsetInPixel = value; }
}
// This is the Y offset in pixels for the image.
public float YOffsetInPixel
{
get { return yOffsetInPixel; }
set { yOffsetInPixel = value; }
}
// This is the angle you would like to rotate the image.
public float RotationAngle
{
get { return rotateAngle; }
set { rotateAngle = value; }
}
// This is the method we needed to override.
protected override void DrawCore(IEnumerable features, GeoCanvas canvas, System.Collections.ObjectModel.Collection labelsInThisLayer, System.Collections.ObjectModel.Collection labelsInAllLayers)
{
// First let's make sure the image isn't null.
if (image != null)
{
// Loop through all of the features being passed in to draw.
foreach (Feature feature in features)
{
// Let's make sure the features being passed in are points or multi points.
WellKnownType shapeWellKnownType = feature.GetWellKnownType();
if (shapeWellKnownType == WellKnownType.Point || shapeWellKnownType == WellKnownType.Multipoint)
{
double currentScale = ExtentHelper.GetScale(canvas.CurrentWorldExtent, canvas.Width, mapUnit);
// Enforce the minimum and maximum scale properties
if (currentScale > maximumScale) { currentScale = maximumScale; }
if (currentScale < minimumScale) { currentScale = minimumScale; }
// Calculate the size of the image based on the scale.
//If the scale is between minimum scale and maximum scale, the size of the image will be calculated according
//to the maximum scale/minimum scale ratio.
//For example:
//Minimum Scale:100,000
//Maximum Scale:200,000
//Minimum Size:10
//Maximum Size:50
//If the scale is 150,000 (mid value between 100,000 and 200,000), the size of the image will be
//30 (mid value between 10 and 50).
float imageSize = (float) (maximumSize - ((currentScale - minimumScale) * (maximumSize - minimumSize)) /
(maximumScale - minimumScale));
// Call the canvas method to draw the image scaled.
PointShape point = new PointShape(feature.GetWellKnownBinary());
canvas.DrawWorldImage(image, point.X, point.Y, imageSize, imageSize, DrawingLevel.LevelOne, xOffsetInPixel, yOffsetInPixel, rotateAngle);
}
}
}
}
}
}
====TestForm.cs====
using System;
using System.Drawing;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;
namespace ScalingImageStyle
{
public partial class TestForm : Form
{
private MapEngine mapEngine = new MapEngine();
private Bitmap bitmap = null;
public TestForm()
{
InitializeComponent();
}
private void TestForm_Load(object sender, EventArgs e)
{
// Set the full extent and the background color
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height);
mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
// Add the static layers to the MapEngine
ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(@"..\..\Data\Countries02.shp", ShapeFileReadWriteMode.ReadOnly);
worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.County1;
worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
mapEngine.StaticLayers.Add("WorldLayer", worldLayer);
ShapeFileFeatureLayer capitalLayer = new ShapeFileFeatureLayer(@"..\..\Data\worldcapitals.shp", ShapeFileReadWriteMode.ReadOnly);
capitalLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Clear();
ScalingImageStyle scalingImageStyle = new ScalingImageStyle(GeographyUnit.DecimalDegree, new GeoImage(@"..\..\Data\Icon.png"), 100000000, 1000000, 50, 10);
capitalLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(scalingImageStyle);
capitalLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
mapEngine.StaticLayers.Add("CapitalLayer", capitalLayer);
DrawImage();
}
private void DrawImage()
{
if (bitmap != null) { bitmap.Dispose(); }
bitmap = new Bitmap(Map.Width, Map.Height);
mapEngine.OpenAllLayers();
mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);
mapEngine.CloseAllLayers();
Map.Image = bitmap;
}
private void ToolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
switch (e.Button.Tag.ToString())
{
case "Zoom In":
mapEngine.CurrentExtent.ScaleDown(50);
break;
case "Zoom Out":
mapEngine.CurrentExtent.ScaleUp(50);
break;
case "Full Extent":
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height);
break;
case "Pan Left":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Left, 20);
break;
case "Pan Right":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20);
break;
case "Pan Up":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20);
break;
case "Pan Down":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Down, 20);
break;
default:
break;
}
DrawImage();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}