实现将地图输出为图片
/// <summary>
/// 地图输出
/// </summary>
/// <param name="map">地图</param>
/// <param name="outputFile">输出文件路径</param>
/// <param name="resolution">输出精度</param>
/// <returns>是否成功</returns>
public static bool MapExport(IMap map, string outputFile, double resolution) {
IActiveView activeView = map as IActiveView;
if (activeView == null) return false;
IExport exporter = null;
string extension = System.IO.Path.GetExtension(outputFile);
switch (extension.ToLower()) {
case ".jpg":
exporter = new ExportJPEGClass() as IExport;
break;
case ".pdf":
exporter = new ExportPDFClass() as IExport;
break;
case ".png":
exporter = new ExportPNGClass() as IExport;
break;
case ".tif":
exporter = new ExportTIFFClass() as IExport;
break;
case ".bmp":
exporter = new ExportBMPClass() as IExport;
break;
case ".gif":
exporter = new ExportGIFClass() as IExport;
break;
default:
throw new Exception("输出图片格式不支持!");
}
try {
double screenResolution = activeView.ScreenDisplay.DisplayTransformation.Resolution;
exporter.ExportFileName = outputFile;
exporter.Resolution = resolution;
tagRECT exportRect = new tagRECT();
exportRect.left = 0;
exportRect.top = 0;
exportRect.right = Convert.ToInt32(activeView.ExportFrame.right * (resolution / screenResolution));
exportRect.bottom = Convert.ToInt32(activeView.ExportFrame.bottom * (resolution / screenResolution));
IEnvelope pixelBoundsEnv = new EnvelopeClass();
pixelBoundsEnv.PutCoords(exportRect.left, exportRect.top, exportRect.right, exportRect.bottom);
exporter.PixelBounds = pixelBoundsEnv;
int hDC = exporter.StartExporting();
activeView.Output(hDC, Convert.ToInt32(resolution), ref exportRect, null, null);
exporter.FinishExporting();
return true;
}
catch {
return false;
}
finally {
exporter.Cleanup();
}
}