Hi,
For our application we require the ability to include high quality maps in our printed reports. The sample to generate a metafile (wiki.thinkgeo.com/wiki/File:...120911.zip) seemed very promising in this regard.
However, I am having trouble limiting the extent of the map.
This is my code to generate a metafile, based on a limiting rectangle defined in the "mapinfo" object (where the scale, center point and width/height of the final map is defined):
public void CopyMapToMetafile(string filename) {
GraphicsGeoCanvas graphicGeoCanvas = new GraphicsGeoCanvas();
Graphics graphic;
Metafile metaFile;
RectangleShape frame;
int W_pxl, H_pxl, scale;
double wrld_W2, wrld_H2; // Half-width and half-height of selected rectangle
// Calculate scale and width/height of selected rectangle (in _meter_)
scale = mapinfo.mapScale; // mapScale is no. of centimeters (world) per centimeter (map)
wrld_W2 = mapinfo.mapWidth*scale / 2000.0; // mapWidth is given in millimeter
wrld_H2 = mapinfo.mapHeight*scale / 2000.0; // mapHeight is given in millimeter
frame = new RectangleShape(mapinfo.centerE - wrld_W2, mapinfo.centerN + wrld_H2,
mapinfo.centerE + wrld_W2, mapinfo.centerN - wrld_H2);
// Calculate size of image, assuming 300 DPI
W_pxl = (int)Math.Round(300.0 * mapinfo.mapWidth / 25.4); // mapWidth is given in millimeter
H_pxl = (int)Math.Round(300.0 * mapinfo.mapHeight / 25.4); // mapHeight is given in millimeter
// Create metafile, and an associated GraphicGeoCanvas
metaFile = new Metafile(filename, Graphics.FromImage(new Bitmap(1, 1)).GetHdc());
graphic = Graphics.FromImage(metaFile);
// Draw each layer to the canvas
graphicGeoCanvas.BeginDrawing(graphic, W_pxl, H_pxl, frame, GeographyUnit.Meter);
foreach (LayerOverlay lovl in map.Overlays) {
foreach (FeatureLayer lay in lovl.Layers) {
lay.Open();
lay.Draw(graphicGeoCanvas, new System.Collections.ObjectModel.Collection<SimpleCandidate>());
lay.Close();
}
}
graphicGeoCanvas.EndDrawing();
metaFile.Dispose();
}
The resulting metafile looks great if, and only if, the limiting rectangle is large enough to include the full extent of all layers. A smaller rectangle results in something like this.
The problem seems to be caused by the GraphicsGeoCanvas not clipping polygons, lines and points to the given rectangle (given as the parameter "frame" in the .BeginDrawing() method).
I have no problems limiting the extent if I draw to a bitmap, but then the size of each map makes the final report file very large.
I also have the option to use the PdfGeoCanvas, which also lets me limit the extent without problems. However, this option requires an additional step to convert the PDF to a format which can be used in Microsoft Word (like EMF or EPS).
Thus, the option to generate EMF directly would be the best, if I could only get it to work...
Any suggestions?