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开发鹰眼图的功能(基础篇)
鹰眼是用于调节全视域范围内主地图显示范围情况的副地图.它体现了地图整体与详细局部的关系. 用户可以通过鼠标单击或者画框等动作实现鹰眼与主地图的交互情况. 鹰眼功能的原理是通过主地图窗口的地图控件和鹰眼 ...
随机推荐
- Hibernatel框架基础使用
Hibernatel框架基础使用 1.简介 1.1.Hibernate框架由来 Struts:基于MVC模式的应用层框架技术 Hibernate:基于持久层的框架(数据访问层使用)! Spring:创 ...
- Angular移除不必要的$watch之性能优化
双向绑定是Angular的核心概念之一,它给我们带来了思维方式的转变:不再是DOM驱动,而是以Model为核心,在View中写上声明式标签.然后,Angular就会在后台默默的同步View的变化到Mo ...
- JavaScript之web通信
web通信,一个特别大的topic,涉及面也是很广的.因最近学习了 javascript 中一些 web 通信知识,在这里总结下.文中应该会有理解错误或者表述不清晰的地方,还望斧正! 一.前言 1. ...
- ABP源码分析三十一:ABP.AutoMapper
这个模块封装了Automapper,使其更易于使用. 下图描述了改模块涉及的所有类之间的关系. AutoMapAttribute,AutoMapFromAttribute和AutoMapToAttri ...
- ajax基础学习
AJAX即"Asynchronous JavaScript and XML",意思是异步JavaScript和XML,是指一种创建交互式网页的网页开发技术. 虽然现在很少有人去自己 ...
- nodejs模块中exports和module.exports的区别
通过Node.js的官方API可以看到Node.js本身提供了很多核心模块 http://nodejs.org/api/ ,这些核心模块被编译成二进制文件,可以require('模块名')去获取:核心 ...
- SSIS 属性:ExecValueVariable
有些Task组件执行完成之后,会产生输出结果,称作Execution Value,例如,Execute SQL Task在执行完成之后,会返回受影响的数据行数.Task组件的Execution Val ...
- 修改Coney主题之侧边栏移位
title: 修改Coney主题之侧边栏移位 date: 2014-12-15 18:09:54 categories: Hexo tags: [hexo,css] --- Coney是一个非常漂亮的 ...
- JavaScript权威设计--命名空间,函数,闭包(简要学习笔记十二)
1.作为命名空间的函数 有时候我们需要声明很多变量.这样的变量会污染全局变量并且可能与别人声明的变量产生冲突. 这时.解决办法是将代码放入一个函数中,然后调用这个函数.这样全局变量就变成了 局部变量. ...
- 多行文本溢出显示省略号(…) text-overflow: ellipsis
详解text-overflow 语法: text-overflow:clip | ellipsis 默认值:clip 适用于:块级容器元素 继承性:无 动画性:否 计算值:指定值 取值: clip:当 ...