using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using ThinkGeo.MapSuite.Core;
namespace CurrentExtentToBitmap
{
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 extent and the background color
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-126.51,50.92,-66.08,22.48), Map.Width, Map.Height);
mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
//Displays the World Map Kit as a background.
ThinkGeo.MapSuite.Core.WorldMapKitLayer worldMapKitLayer = new ThinkGeo.MapSuite.Core.WorldMapKitLayer();
mapEngine.StaticLayers.Add(worldMapKitLayer);
//Loads and displays the point based shapefile for the main cities.
ShapeFileFeatureLayer Layer1 = new ShapeFileFeatureLayer(@"../../Data/MajorCities.shp");
Layer1.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.City1;
Layer1.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.CreateMaskTextStyle("AREANAME", new GeoFont("Arial", 10), new GeoSolidBrush(GeoColor.StandardColors.Black),
new AreaStyle(new GeoPen(GeoColor.StandardColors.Transparent), new GeoSolidBrush(GeoColor.StandardColors.LightGoldenrodYellow)), 8, 0);
Layer1.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
mapEngine.StaticLayers.Add(Layer1);
DrawImage();
}
private void btnToBitmap_Click(object sender, EventArgs e)
{
//Saves the bitmap for the current extent of the map.
bitmap.Save(@"../../data/mymap.bmp");
//Creates the world file so that the bitmap is georeferenced according to the standard
// http://en.wikipedia.org/wiki/World_file
//Make sure the world file has the same name as the bitmap with the bpw extention so that it can be
//read correctly by Map Suite controls or other mapping controls.
using (StreamWriter worldFile = new StreamWriter(@"../../data/mymap.bpw"))
{
//Calculates the pixel size in the x-direction in map unit.
double PixelSizeX = mapEngine.CurrentExtent.Width / Map.Width;
//Calculates the pixel size in the y-direction in map unit.
double PixelSizeY = mapEngine.CurrentExtent.Height / Map.Height;
//Calculates the x-coordinate of the center of the upper left pixel.
double XCoord = mapEngine.CurrentExtent.UpperLeftPoint.X + (PixelSizeX / 2);
//Calculates the y-coordinate of the center of the upper left pixel.
double YCoord = mapEngine.CurrentExtent.UpperLeftPoint.Y - (PixelSizeY / 2);
//Writes those parameters to the world file.
worldFile.WriteLine(PixelSizeX.ToString());
worldFile.WriteLine("0");
worldFile.WriteLine("0");
worldFile.WriteLine(PixelSizeY.ToString());
worldFile.WriteLine(XCoord.ToString());
worldFile.WriteLine(YCoord.ToString());
}
}
private void DrawImage()
{
if (bitmap != null) { bitmap.Dispose(); }
bitmap = new Bitmap(Map.Width, Map.Height);
mapEngine.OpenAllLayers();
mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);
mapEngine.DrawDynamicLayers(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();
}
private void Map_MouseMove(object sender, MouseEventArgs e)
{
//Displays the X and Y in screen coordinates.
statusStrip1.Items["toolStripStatusLabelScreen"].Text = "X:" + e.X + " Y:" + e.Y;
//Gets the PointShape in world coordinates from screen coordinates.
PointShape pointShape = ExtentHelper.ToWorldCoordinate(mapEngine.CurrentExtent, new ScreenPointF(e.X, e.Y), Map.Width, Map.Height);
//Displays world coordinates.
statusStrip1.Items["toolStripStatusLabelWorld"].Text = "(world) X:" + Math.Round(pointShape.X, 4) + " Y:" + Math.Round(pointShape.Y, 4);
}
}
}