public static void ExportMapEx(string filepath, int resolution, AxMapControl curMapControl)
{
try
{
// 从Style文件中获取指定的指北针
string stylefilePath = Application.StartupPath + @"\style\testArrow.ServerStyle";
INorthArrow pNorthArrow = GetSymbol(stylefilePath, "North Arrows", "ESRI North 2") as INorthArrow;
if (pNorthArrow == null) return;
// 设置指北针的位置及大小
ESRI.ArcGIS.Geometry.IEnvelope envelope3 = new ESRI.ArcGIS.Geometry.EnvelopeClass();
envelope3.PutCoords(2, 2, 5, 5);
// 设置当前活动视图,m_MapDocument为当前打开的地图文档
IActiveView pCurActiveView = MyGIS.Forms.MainForm.m_MapDocument.PageLayout as IActiveView;
MyGIS.Forms.MainForm.m_MapDocument.SetActiveView(pCurActiveView);
IGraphicsContainer pGraphicContainer = (IGraphicsContainer)MyGIS.Forms.MainForm.m_MapDocument.PageLayout;
IMapFrame mapFrame = pGraphicContainer.FindFrame(pCurActiveView.FocusMap) as IMapFrame ;
IMapSurroundFrame mapSurroundFrame = new MapSurroundFrameClass();
mapSurroundFrame.MapFrame = mapFrame;
mapSurroundFrame.MapSurround = pNorthArrow as IMapSurround;
IElement element = (IElement)mapSurroundFrame;
element.Geometry = envelope3;
// 添加元素
element.Activate(pCurActiveView.ScreenDisplay);
pGraphicContainer.AddElement(element, 0);
//pCurActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, mapSurroundFrame, null);
// 这里分辨率分两种情况:96dpi和300dpi
string extension = System.IO.Path.GetExtension(filepath);
IExport pExporter = null;
if (extension == ".bmp")
{
pExporter = new ExportBMPClass();
}
else if (extension == ".jpg")
{
pExporter = new ExportJPEGClass();
}
else if (extension == ".png")
{
pExporter = new ExportPNGClass();
}
else if (extension == ".gif")
{
pExporter = new ExportGIFClass();
}
else if (extension == ".pdf")
{
pExporter = new ExportPDFClass();
}
else if (extension == ".tif")
{
pExporter = new ExportTIFFClass();
}
pExporter.ExportFileName = filepath;
tagRECT deviceRECT = default(tagRECT);
if (resolution < 96)
{
return;
}
// 区分设置分辨率
if (resolution == 96)
{
pExporter.Resolution = resolution;
deviceRECT = pCurActiveView.ExportFrame;
IEnvelope pDriverBounds = new EnvelopeClass();
pDriverBounds.PutCoords(deviceRECT.left, deviceRECT.bottom, deviceRECT.right, deviceRECT.top);
pExporter.PixelBounds = pDriverBounds;
}
else
{
System.Int32 screenResolution = 96;
pExporter.Resolution = resolution;
deviceRECT.left = 0;
deviceRECT.top = 0;
deviceRECT.right = (int)(pCurActiveView.ExportFrame.right * (resolution / screenResolution));
deviceRECT.bottom = (int)(pCurActiveView.ExportFrame.bottom * (resolution / screenResolution));
ESRI.ArcGIS.Geometry.IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass();
envelope.PutCoords(deviceRECT.left, deviceRECT.top, deviceRECT.right, deviceRECT.bottom);
pExporter.PixelBounds = envelope;
}
//ITrackCancel pCancel = new CancelTrackerClass();
//curMapControl.ActiveView.Output(pExporter.StartExporting(), Convert.ToInt32(lScreenResolution), ref deviceRECT, curMapControl.ActiveView.Extent, pCancel);
pCurActiveView.Output(pExporter.StartExporting(), Convert.ToInt32(resolution), ref deviceRECT, null, null);
pExporter.FinishExporting();
pExporter.Cleanup();
MessageBox.Show("导出图像成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
MessageBox.Show("导出异常: \n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}