====== Source Code ServicesEditionSample RasterLayerToBitmap CS 100903.zip ======
====Program.cs====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace RasterLayerToBitmap
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TestForm());
}
}
}
====TestForm.cs====
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 RasterLayerToBitmap
{
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(-190,100,190,-100), Map.Width, Map.Height);
mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
//Adds the Mr Sid to the map
MrSidRasterLayer rasterLayer = new MrSidRasterLayer(@"../../data/World.sid");
mapEngine.StaticLayers.Add(rasterLayer);
DrawImage();
}
private void btnToBitmap_Click(object sender, EventArgs e)
{
//Calculates the size of the image for the raster layer at the current extent
//This means that if you zoom out, you will get a smaller image and if you zoom in, you will get a larger image.
RasterLayer rasterLayer = (RasterLayer)mapEngine.StaticLayers[0];
rasterLayer.Open();
float imageWidth = ExtentHelper.GetScreenDistanceBetweenTwoWorldPoints(mapEngine.CurrentExtent, rasterLayer.GetBoundingBox().UpperLeftPoint,
rasterLayer.GetBoundingBox().UpperRightPoint, Map.Width, Map.Height);
float imageHeight = ExtentHelper.GetScreenDistanceBetweenTwoWorldPoints(mapEngine.CurrentExtent, rasterLayer.GetBoundingBox().UpperLeftPoint,
rasterLayer.GetBoundingBox().LowerLeftPoint, Map.Width, Map.Height);
//Gets the GeoImage of the raster layer at the calculated size.
GeoImage geoImage = rasterLayer.ImageSource.GetImage(rasterLayer.GetBoundingBox(),(int)(imageWidth), (int) (imageHeight));
//Gets the bitmap from the GeoImage and saves it.
Bitmap myBitmap = null;
lock (geoImage)
{
Stream imageStream = geoImage.GetImageStream(new GdiPlusGeoCanvas());
myBitmap = new Bitmap(imageStream);
}
myBitmap.Save(@"../../data/world.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/world.bpw"))
{
//Calculates the pixel size in the x-direction in map unit.
double PixelSizeX = rasterLayer.GetBoundingBox().Width / myBitmap.Width;
//Calculates the pixel size in the y-direction in map unit.
double PixelSizeY = rasterLayer.GetBoundingBox().Height / myBitmap.Height;
//Calculates the x-coordinate of the center of the upper left pixel.
double XCoord = rasterLayer.GetBoundingBox().UpperLeftPoint.X + (PixelSizeX / 2);
//Calculates the y-coordinate of the center of the upper left pixel.
double YCoord = rasterLayer.GetBoundingBox().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());
}
rasterLayer.Close();
}
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);
}
}
}