ArcGIS Engine开发之地图导出
关于地图导出的方法有很多,但是核心技术就那么一点。下面是从项目实战中总结的一部分地图导出的方法:(以全域导出和区域导出为例)
1.由于地图导出用到的函数和方法容易重复,增加了工作量故首先将其进行封装成类(ExportMap类):用到的主要接口为:IActiveView(活动视图接口)、IGeometry(几何接口)、IRgbColor(颜色接口)、IElement(要素接口)等。
具体的封装代码如下:
class ExportMap
{
#region 输出视图
public static void ExportView(IActiveView view, IGeometry pGeo, int Outputresoultion, int Width, int Height, string ExpPath, bool bRegion)
{
IExport pExport = null;
tagRECT exportrect = new tagRECT();
IEnvelope pEnvelop = pGeo.Envelope;
string sType = System.IO.Path.GetExtension(ExpPath);
switch (sType)
{
case ".jpg":
pExport = new ExportJPEGClass();
break;
case ".bmp":
pExport = new ExportBMPClass();
break;
case ".gif":
pExport = new ExportGIFClass();
break;
case ".tif":
pExport = new ExportTIFFClass();
break;
case ".png":
pExport = new ExportPNGClass();
break;
case ".pdf":
pExport = new ExportPDFClass();
break;
default:
MessageBox.Show("没有输出格式,默认JPEG");
pExport = new ExportJPEGClass();
break;
}
pExport.ExportFileName = ExpPath;
exportrect.left = ;
exportrect.top = ;
exportrect.right = Width;
exportrect.bottom = Height;
if (bRegion)
{
view.GraphicsContainer.DeleteAllElements();
view.Refresh();
}
IEnvelope envelop = new EnvelopeClass();
envelop.PutCoords((double)exportrect.left, (double)exportrect.top, (double)exportrect.right, (double)exportrect.bottom);
pExport.PixelBounds = envelop;
view.Output(pExport.StartExporting(), Outputresoultion, ref exportrect, pEnvelop, null);
pExport.FinishExporting();
pExport.Cleanup();
}
#endregion
#region 获取RGB颜色
private static IRgbColor GetRgbColor(int intR,int intG,int intB)
{
IRgbColor pRgbColor=null;
if(intR<||intR>||intG<||intG>||intB<||intB>)
{
return pRgbColor;
}
pRgbColor=new RgbColorClass();
pRgbColor.Red=intR;
pRgbColor.Green=intG;
pRgbColor.Blue=intB;
return pRgbColor;
}
#endregion
#region 创建图形元素
/// <summary>
///
/// </summary>
/// <param name="pGeomentry">几何图形</param>
/// <param name="lineColor">边框颜色</param>
/// <param name="fillColor">填充颜色</param>
/// <returns></returns>
public static IElement CreateElement(IGeometry pGeomentry, IRgbColor lineColor, IRgbColor fillColor)
{
if (pGeomentry == null || lineColor == null || fillColor == null)
{
return null;
}
IElement pElem = null;
try
{
if (pGeomentry is IEnvelope) pElem = new RectangleElementClass();
else if (pGeomentry is IPolygon)
pElem = new PolygonElementClass();
else if (pGeomentry is ICircularArc)
{
ISegment pSegCircle = pGeomentry as ISegment;
ISegmentCollection pSegColl = new PolygonClass();
object o = Type.Missing;
pSegColl.AddSegment(pSegCircle, ref o, ref o);
IPolygon pPolygon = pSegCircle as IPolygon;
pGeomentry = pPolygon as IGeometry;
pElem = new CircleElementClass();
}
else if (pGeomentry is IPolyline)
pElem = new LineElementClass();
if (pElem == null)
return null;
pElem.Geometry = pGeomentry;
IFillShapeElement pFElem = pElem as IFillShapeElement;
ISimpleFillSymbol pSymbol = new SimpleFillSymbolClass();
pSymbol.Color=fillColor ;
pSymbol.Outline.Color=lineColor;
pSymbol.Style = esriSimpleFillStyle.esriSFSCross;//图形元素的样式
if (pSymbol == null)
{
return null;
}
pFElem.Symbol = pSymbol;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message );
}
return pElem;
}
#endregion
#region 视图窗口绘制几何图形元素
/// <summary>
///
/// </summary>
/// <param name="pGeometry">几何图形</param>
/// <param name="activeView">活动视图</param>
public static void AddElement(IGeometry pGeometry,IActiveView activeView)
{
IRgbColor fillColor=GetRgbColor(,,);
IRgbColor lineColor=GetRgbColor(,,);
IElement pEle=CreateElement(pGeometry,lineColor,fillColor );//调用图形元素的函数
IGraphicsContainer pGC = activeView.GraphicsContainer;
if (pGC != null)
{
pGC.AddElement(pEle, );
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, pEle, null);
}
}
#endregion
#region 全域导出
/// <summary>
/// 全域导出
/// </summary>
/// <param name="OutputResolution">输出分辨率</param>
/// <param name="ExpPath">输出路径</param>
/// <param name="view">视图</param>
public static void ExportActiveView(int OutputResolution, string ExpPath, IActiveView view)
{
IExport pExport = null;
tagRECT exportRect;
IEnvelope envelop2 = view.Extent;
int num = (int)Math.Round(view.ScreenDisplay.DisplayTransformation.Resolution);
string sType = System.IO.Path.GetExtension(ExpPath);
switch (sType)
{
case ".jgp":
pExport = new ExportJPEGClass();
break;
case ".bmp":
pExport = new ExportBMPClass();
break;
case ".gif":
pExport = new ExportGIFClass();
break;
case ".tif":
pExport = new ExportTIFFClass();
break;
case ".png":
pExport = new ExportPNGClass();
break;
case ".pdf":
pExport = new ExportPDFClass();
break;
default: MessageBox.Show("No Export Foemat,the default format is JPEG!");
pExport = new ExportJPEGClass();
break;
}
pExport.ExportFileName = ExpPath;
exportRect.left = ; exportRect.top = ;
exportRect.right = (int)Math.Round((double)(view.ExportFrame.right * (((double)OutputResolution) / ((double)num))));
exportRect.bottom = (int)Math.Round((double)(view.ExportFrame.bottom * (((double)OutputResolution) / ((double)num))));
IEnvelope envelop = new EnvelopeClass();
envelop.PutCoords((double)exportRect.left, (double)exportRect.top, (double)exportRect.right, (double)exportRect.bottom);
pExport.PixelBounds = envelop;
view.Output(pExport.StartExporting(), OutputResolution, ref exportRect, envelop2, null);
pExport.FinishExporting();
pExport.Cleanup();
}
#endregion
#region 区域导出
/// <summary>
/// 区域导出
/// </summary>
/// <param name="pGeo">输出的图形</param>
/// <param name="OutputResolution">输出的范围</param>
/// <param name="ExpPath">输出路径</param>
/// <param name="view">视图</param>
public static void ExportRegion(IGeometry pGeo, int OutputResolution, string ExpPath, IActiveView view)
{
IExport export = null;
IWorldFileSettings setting = null;
IEnvelope envelope2 = pGeo.Envelope;
string str = ExpPath.Substring(ExpPath.Length - , ).ToUpper();
switch (str)
{
case "JPG":
setting = new ExportJPEGClass();
export = new ExportJPEGClass();
setting = export as IWorldFileSettings;
setting.MapExtent = envelope2;
setting.OutputWorldFile = false;
break;
case "BMP":
setting = new ExportBMPClass();
export = new ExportBMPClass();
setting = export as IWorldFileSettings;
setting.MapExtent = envelope2;
setting.OutputWorldFile = false;
break;
case "TIF":
setting = new ExportTIFFClass();
export = new ExportTIFFClass();
setting = export as IWorldFileSettings;
setting.MapExtent = envelope2;
setting.OutputWorldFile = false;
break;
case "PNG":
setting = new ExportPNGClass();
export = new ExportPNGClass();
setting = export as IWorldFileSettings;
setting.MapExtent = envelope2;
setting.OutputWorldFile = false;
break;
default: break;
}
if (setting == null)
{
export.ExportFileName = ExpPath;
int num = (int)Math.Round(view.ScreenDisplay.DisplayTransformation.Resolution);
tagRECT grect2 = new tagRECT();//实例化矩形
IEnvelope envelop3 = new EnvelopeClass();
view.ScreenDisplay.DisplayTransformation.TransformRect(envelope2, ref grect2, );
grect2.left = ;
grect2.top = ;
grect2.right = (int)Math.Round((double)(grect2.right - grect2.left) * (((double)OutputResolution) / ((double)num)));
grect2.bottom = (int)Math.Round((double)((grect2.bottom - grect2.top) * (((double)OutputResolution) / ((double)num))));
envelop3.PutCoords((double)grect2.left, (double)grect2.top, (double)grect2.right, (double)grect2.bottom);
export.PixelBounds = envelop3;
view.GraphicsContainer.DeleteAllElements();
view.Output(export.StartExporting(), OutputResolution, ref grect2, envelope2, null);
export.FinishExporting();
export.Cleanup();
AddElement(pGeo, view);
} }
#endregion
}
2.添加输出设置窗体,分别有,输出图片的高、宽、分辨率、输出保存路径、导出按钮。

具体的设置代码如下:
public partial class ExportMapForm : DevExpress.XtraEditors.XtraForm
{
//定义全局变量
private string pSavePath = "";
private IActiveView pActiveView;
private IGeometry pGeometry = null;
#region 只读属性,地图导出空间图形
public IGeometry GetGeometry
{
set { pGeometry = value; }
}
private bool bRegion = true;
#endregion
/// <summary>
/// 只读属性,是全域导出还是区域导出
/// </summary>
public bool IsRegion
{
set { bRegion = value; }
} //获取主窗口的MapControl控件
public ExportMapForm(AxMapControl mainAxMapControl)
{
InitializeComponent(); pActiveView = mainAxMapControl.ActiveView; } private void ExportMapForm_Load(object sender, EventArgs e)
{ }
#region 输入窗口的大小
private void InitFormSize()
{
cboResoultion.Text = pActiveView.ScreenDisplay.DisplayTransformation.Resolution.ToString();
cboResoultion.Items.Add(cboResoultion.Text);
if (bRegion)
{
IEnvelope pEnvelop = pGeometry.Envelope;
tagRECT pRECT = new tagRECT();
pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnvelop, ref pRECT,);
if (cboResoultion.Text != "")
{
txtWidth.Text = pRECT.right.ToString();
txtHeight.Text = pRECT.bottom.ToString();
}
}
else
{
if (cboResoultion.Text != "")
{
txtWidth.Text = pActiveView.ExportFrame.right.ToString();
txtHeight.Text = pActiveView.ExportFrame.bottom.ToString();
}
}
}
#endregion
#region combox的ChangeIndex事件
private void cboResoultion_SelectedIndexChanged(object sender, EventArgs e)
{
double num = (int)Math.Round(pActiveView.ScreenDisplay.DisplayTransformation.Resolution);
if (cboResoultion.Text == "")
{
txtWidth.Text = "";
txtHeight.Text = "";
return;
}
if (bRegion)
{
IEnvelope pEnvelop = pGeometry.Envelope;
tagRECT pRECT = new tagRECT();
pActiveView.ScreenDisplay.DisplayTransformation.TransformRect(pEnvelop, ref pRECT,);
if (cboResoultion.Text != "")
{
txtWidth.Text = Math.Round((double)(pRECT.right * (double.Parse(cboResoultion.Text) / (double)num))).ToString();
}
}
else
{
txtWidth.Text = Math.Round((double)(pActiveView.ExportFrame.right * (double.Parse(cboResoultion.Text) / (double)num))).ToString();
txtHeight.Text = Math.Round((double)(pActiveView.ExportFrame.bottom * (double.Parse(cboResoultion.Text) / (double)num))).ToString();
}
}
#endregion
#region 保存按钮的单击事件
private void btnExPath_Click(object sender, EventArgs e)
{
SaveFileDialog sfdExportMap = new SaveFileDialog();
sfdExportMap.DefaultExt = "jpg|bmp|gif|tif|png|pdf";
sfdExportMap.Filter = "JPGE 文件(*.jpg)|*.jpg|BMP 文件(*.bmp)|*.bmp|GIF 文件(*.gif)|*.gif|TIF 文件(*.tif)|*.tif|PNG 文件(*.png)|*.png|PDF 文件(*.pdf)|*.pdf";
sfdExportMap.OverwritePrompt = true;//重复写入时提示错误
sfdExportMap.Title = "保存为";
txtExPath.Text = "";
if (sfdExportMap.ShowDialog() != DialogResult.Cancel)
{
pSavePath = sfdExportMap.FileName;
txtExPath.Text = sfdExportMap.FileName;
}
}
#endregion
#region 导出按钮单击事件
private void btnExPort_Click(object sender, EventArgs e)
{
if (txtExPath.Text == "")
{
MessageBox.Show("请先确定导出路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else if (cboResoultion.Text == "")
{
if (txtExPath.Text == "")
{
MessageBox.Show("请输入分辨率!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
else if (Convert.ToInt16(cboResoultion.Text) == )
{
MessageBox.Show("请正确输入分辨率!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
try
{
int resoultion = int.Parse(cboResoultion.Text);//输出图片的分辨率
int width = int.Parse(cboResoultion.Text);//输出图片的宽度
int height = int.Parse(cboResoultion.Text);//输出图片的高度
ExportMap.ExportView(pActiveView, pGeometry, resoultion, width, height, pSavePath, bRegion);
pActiveView.GraphicsContainer.DeleteAllElements();
pActiveView.Refresh();
MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception)
{
MessageBox.Show("导出失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
#endregion
#region 取消按钮的单击事件
private void btnCancel_Click(object sender, EventArgs e)
{
//局部导出时没有导出图像就退出
pActiveView.GraphicsContainer.DeleteAllElements();
pActiveView.Refresh();
Dispose();
}
#endregion
#region 图片导出窗口Close事件
private void ExportMapForm_FormClosed(object sender, FormClosedEventArgs e)
{
//局部导出时没有导出图像就关闭
pActiveView.GraphicsContainer.DeleteAllElements();
pActiveView.Refresh();
Dispose(); }
#endregion
3.在main窗体中进行实例化输出窗体:private ExportMapForm frmExpMap = null;
4.在MainMap Control_OnMouseDown中进行写入case:(只适合区域导出,单击选择边框)
#region 地图的区域导出
case "ExportRegion":
//删除视图中的数据
mainMapControl.ActiveView.GraphicsContainer.DeleteAllElements();
mainMapControl.ActiveView.Refresh();
IPolygon pPolygon = DrawPolygon(mainMapControl);
if (pPolygon == null) return;
ExportMap.AddElement(pPolygon, mainMapControl.ActiveView);
if (frmExpMap == null || frmExpMap.IsDisposed)
{
frmExpMap = new ExportMapForm(mainMapControl);
}
frmExpMap.IsRegion = true;
frmExpMap.GetGeometry = pPolygon as IGeometry;
frmExpMap.Show();
frmExpMap.Activate();
break;
#endregion
5.全域导出和区域导出按钮的单击事件代码:
#region 地图导出之全域导出
private void btnExportMap_ItemClick(object sender, ItemClickEventArgs e)
{
if (frmExpMap == null || frmExpMap.IsDisposed)
{
frmExpMap = new ExportMapForm(mainMapControl);
}
frmExpMap.IsRegion = false;
frmExpMap.GetGeometry = mainMapControl.ActiveView.Extent;
frmExpMap.Show();
frmExpMap.Activate();
}
#endregion
#region 地图导出之区域导出
private void btnExportRegionMap_ItemClick(object sender, ItemClickEventArgs e)
{
mainMapControl.CurrentTool = null;
mainMapControl.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
pMouseOperate = "ExportRegion";
}
#endregion
ArcGIS Engine开发之地图导出的更多相关文章
- ArcGIS Engine开发之地图基本操作(4)
ArcGIS Engine开发中数据库的加载 1.加载个人地理数据库数据 个人地理数据库(Personal Geodatabase)使用Miscrosoft Access文件(*.mdb)进行空间数据 ...
- ArcGIS Engine开发之地图基本操作(3)
地图数据的加载 一.加载Shapefile数据 Shapefile文件是目前主流的一种空间数据的文件存储方式,也是不同GIS软件进行数据格式转换常用的中间格式.加载Shapefile数据的方式有两种: ...
- ArcGIS Engine开发之地图基本操作(2)
地图数据的加载 1.加载地图文档 ArcGIS Engine支持加载多种类型的数据,有矢量数据的Coverage.Shapefile.dwg/dxf文件,栅格数据的BMP.GRID.控件数据库等.很多 ...
- ArcGIS Engine开发之地图基本操作(1)
ArcGIS提供的各类数据形式以及相应接口 1. 空间数据 在GIS软件中,空间数据有多种不同的形式存在.按照不同的划分标准可以分为矢量数据和栅格数据.GIS格式数据和非GIS格式数据(CAD格式). ...
- ArcGIS Engine开发之地图浏览
地图的浏览功能包括缩放.移动.量测旋转等. 1.放大与缩小 无论是放大还是缩小,都是通过改变MapControl中当前视图的范围Extent属性来实现的,主要用到包络线(Envelope)类. 包络线 ...
- ArcGIS Engine开发前基础知识(2)
ArcGIS基本控件简介 ArcGIS Engine控件是一组可视化的开发组件,每个ArcGIS Engine控件都是一个COM组件.这些组件包括MapControl,PageLayoutContro ...
- C#,ArcGIS Engine开发入门教程
C#,ArcGIS Engine开发入门教程 转自:http://blog.csdn.net/yanleigis/article/details/2233674 目录(?)[+] 五实现 一 加载A ...
- ArcGIS Engine开发基础总结(一)
标准Engine功能 地图浏览 地图制作 数据查询 数据分析 及 所有的开发控件 —MapControl, PageLayout, Toolbar, TOC, ArcReader 对所有矢量和栅 ...
- ArcGIS Engine开发鹰眼图的功能(基础篇)
鹰眼是用于调节全视域范围内主地图显示范围情况的副地图.它体现了地图整体与详细局部的关系. 用户可以通过鼠标单击或者画框等动作实现鹰眼与主地图的交互情况. 鹰眼功能的原理是通过主地图窗口的地图控件和鹰眼 ...
随机推荐
- TCP/IP之Nagle算法与40ms延迟
Nagle算法是针对网络上存在的微小分组可能会在广域网上造成拥塞而设计的.该算法要求一个TCP连接上最多只能有一个未被确认的未完成的小分组,在该分组确认到达之前不能发送其他的小分组.同时,TCP收集这 ...
- Windows下Nginx配置SSL实现Https访问(包含证书生成)
Vincent.李 Windows下Nginx配置SSL实现Https访问(包含证书生成) Windows下Nginx配置SSL实现Https访问(包含证书生成) 首先要说明为什么要实现https ...
- embedding mono实战笔录(一)
最近在给自己的服务器节点添加脚本功能,考虑到 执行性能.开发效率.调试效率.可维护性.严谨性 五大要素,最终选用C#作为脚本语言,并使用mono作为中间层,使其具备跨平台特性,以备具有在Windows ...
- 新应用上线 Snippet
Snippet 是一款代码片段收集工具,经过一天三夜的开发终于上线了. 应用地址:snippets.barretlee.com 源码地址:barretlee/snippets 由于使用原生 JS 开发 ...
- C语言图形库简单对比及EGE库的安装小手册
近期在琢磨C语言的图形库,发现主要有如下几种选择: Turbo C 的graphics库 SDL EasyX EGE 1. 普遍认为Graphics库太老了,而且TurboC本身使用比较麻烦,网上一边 ...
- [原创]关于mybatis中一级缓存和二级缓存的简单介绍
关于mybatis中一级缓存和二级缓存的简单介绍 mybatis的一级缓存: MyBatis会在表示会话的SqlSession对象中建立一个简单的缓存,将每次查询到的结果结果缓存起来,当下次查询的时候 ...
- mysql交互协议解析——mysql包基础数据、mysql包基本格式
mysql交互协议是开发mysql周边组件常用的协议,如JDBC,libmysql等等. 在此我们要认识到mysql交互协议其实是半双工的交互协议,至于为什么,这里就先挖个小坑,以后再填. 在探讨my ...
- 读书笔记--SQL必知必会11--使用子查询
11.1 子查询 查询(query),任何SQL语句都是查询.但此术语一般指SELECT语句. SQL还允许创建子查询(subquery),即嵌套在其他查询中的查询. 作为子查询的SELECT语句只能 ...
- 移动开发那些坑之——safari mobile click事件的冒泡bug
今天在iphone6 plus的safari上测试这么一段代码: <script> $(document).on('click','.callApp', function() { aler ...
- Hadoop入门学习笔记---part4
紧接着<Hadoop入门学习笔记---part3>中的继续了解如何用java在程序中操作HDFS. 众所周知,对文件的操作无非是创建,查看,下载,删除.下面我们就开始应用java程序进行操 ...