VSC# ArcGIS 二次开发--出图工具:添加图名、比例尺、指北针、图例,导出图片或文件、打印地图
生活随笔
收集整理的這篇文章主要介紹了
VSC# ArcGIS 二次开发--出图工具:添加图名、比例尺、指北针、图例,导出图片或文件、打印地图
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這是我們GIS課程的結(jié)課作業(yè),只能說(shuō),大心血
鏈接在最后,但是還是希望大家可以看看代碼,可以留言相互交流學(xué)習(xí)。
?
目錄
鏈接在最后,但是還是希望大家可以看看代碼,可以留言相互交流學(xué)習(xí)。
添加圖名
指北針
比例尺(類似指北針。從樣式庫(kù)里添加)
圖例
指北針窗體
導(dǎo)出地圖
打印地圖
?
添加圖名
public void AddTitle(IPageLayout pageLayout, String s){//找到PageLayoutIPageLayout pPageLayout = mainPageLayoutControl1.PageLayout;//找到元素容器IGraphicsContainer pGraphicsContainer = pPageLayout as IGraphicsContainer;//創(chuàng)建元素ITextElement pTextElement = new TextElementClass();pTextElement.Text = s;ITextSymbol pTextSymbol = new TextSymbolClass();//Text的符號(hào)樣式pTextSymbol.Size = 30;pTextSymbol.Color = GetRGB(0, 0, 0);pTextElement.Symbol = pTextSymbol;//設(shè)置位置 ? ? ? ? ? ? ? ? ? ? ? ?IElement pElement = pTextElement as IElement;pElement.Geometry = mainPageLayoutControl1.TrackRectangle();//將元素添加到容器中pGraphicsContainer.AddElement(pElement, 0);//刷新mainPageLayoutControl1.Refresh(); }?
指北針
IMapFrame pMapFrame = ((IActiveView)this.mainPageLayoutControl1.PageLayout).GraphicsContainer.FindFrame(this.mainPageLayoutControl1.ActiveView.FocusMap) as IMapFrame;IElement pElement = pMapFrame as IElement;UID pID = new UIDClass();pID.Value = "esriCarto.MarkerNorthArrow";IEnvelope pEnv = new EnvelopeClass();pEnv.PutCoords(16, 24, 22, 26);AddNorthArrow NForm = new AddNorthArrow(mainPageLayoutControl1.ActiveView, esriSymbologyStyleClass.esriStyleClassNorthArrows);//axPageLayoutControl1.ActiveView, esriSymbologyStyleClass.esriStyleClassNorthArrowsNForm.ShowDialog();IStyleGalleryItem styleGalleryItem = NForm.m_styleGalleryItem;NForm.Dispose();if (styleGalleryItem == null) return;IMapSurround pMapSurround = this.CreateSurround(pID, pEnv, styleGalleryItem, pMapFrame, this.mainPageLayoutControl1.PageLayout);this.mainPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);比例尺(類似指北針。從樣式庫(kù)里添加)
IMapFrame pMapFrame = ((IActiveView)this.mainPageLayoutControl1.PageLayout).GraphicsContainer.FindFrame(this.mainPageLayoutControl1.ActiveView.FocusMap) as IMapFrame;IElement pElement = pMapFrame as IElement;UID pID = new UIDClass();pID.Value = "esriCarto.ScaleLine";IEnvelope pEnv = new EnvelopeClass();pEnv.PutCoords(2, 3, 3, 5);AddScalebar SForm = new AddScalebar(mainPageLayoutControl1.ActiveView, esriSymbologyStyleClass.esriStyleClassScaleBars);SForm.ShowDialog();IStyleGalleryItem styleGalleryItem = SForm.m_styleGalleryItem;SForm.Dispose();if (styleGalleryItem == null) return;IMapSurround pMapSurround = this.CreateSurround(pID, pEnv, styleGalleryItem, pMapFrame, this.mainPageLayoutControl1.PageLayout);this.mainPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);圖例
public void AddLegend(IPageLayout pageLayout){//獲取axPageLayoutControl1的圖形容器IGraphicsContainer graphicsContainer = mainPageLayoutControl1.GraphicsContainer;//獲取axPageLayoutControl1空間里面顯示的地圖圖層IMapFrame mapFrame = (IMapFrame)graphicsContainer.FindFrame(mainPageLayoutControl1.ActiveView.FocusMap);if (mapFrame == null) return;//創(chuàng)建圖例UID uID = new UIDClass();//創(chuàng)建UID作為該圖例的唯一標(biāo)識(shí)符,方便創(chuàng)建之后進(jìn)行刪除、移動(dòng)等操作uID.Value = "esriCarto.Legend";IMapSurroundFrame mapSurroundFrame = mapFrame.CreateSurroundFrame(uID, null);if (mapSurroundFrame == null) return;if (mapSurroundFrame.MapSurround == null) return;mapSurroundFrame.MapSurround.Name = "Legend";IEnvelope envelope = new EnvelopeClass();envelope.PutCoords(16, 3, 20, 5);//設(shè)置圖例擺放位置(原點(diǎn)在axPageLayoutControl左下角)IElement element = (IElement)mapSurroundFrame;element.Geometry = envelope;//將圖例轉(zhuǎn)化為幾何要素添加到axPageLayoutControl1,并刷新頁(yè)面顯示mainPageLayoutControl1.AddElement(element, Type.Missing, Type.Missing, "Legend", 0);mainPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);}?
指北針窗體
using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.Geometry; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace WindowsFormsApplication3 {public partial class AddNorthArrow : Form{private IActiveView mActiveView;private IFeatureLayer mFeatureLayer;public IStyleGalleryItem m_styleGalleryItem;private esriSymbologyStyleClass mStyleClass;public AddNorthArrow(IFeatureLayer pLayer, IActiveView pActiveView){InitializeComponent();this.Text = "指北針樣式選擇";mFeatureLayer = pLayer;mActiveView = pActiveView;}public AddNorthArrow(IActiveView pActiveView, esriSymbologyStyleClass styleClass){mActiveView = pActiveView;mStyleClass = styleClass;InitializeComponent();}private void Form2_Load(object sender, EventArgs e){axSymbologyControl1.LoadStyleFile("D:\。。。。\ESRI.ServerStyle"); axSymbologyControl1.StyleClass = esriSymbologyStyleClass.esriStyleClassNorthArrows;//Select the color ramp itemaxSymbologyControl1.GetStyleClass(axSymbologyControl1.StyleClass).SelectItem(0); }private void axSymbologyControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ISymbologyControlEvents_OnMouseDownEvent e){}private void button1_Click(object sender, EventArgs e){this.Hide(); }private void button2_Click(object sender, EventArgs e){m_styleGalleryItem = null;//hide the formthis.Hide(); }private void axSymbologyControl1_OnItemSelected(object sender, ISymbologyControlEvents_OnItemSelectedEvent e){m_styleGalleryItem = (IStyleGalleryItem)e.styleGalleryItem;}}}導(dǎo)出地圖
private void btnPrint_Click(object sender, EventArgs e)//點(diǎn)擊打印Button響應(yīng){//創(chuàng)建保存文件對(duì)話框的實(shí)例SaveFileDialog SaveFile = new SaveFileDialog();//根據(jù)掩碼文本框?qū)?duì)話框初始化switch (comboBox1.SelectedIndex){case 0:SaveFile.Filter = "BPM文件(*.bmp)|*.bmp";SaveFile.Title = "保存bmb文件";break;case 2:SaveFile.Filter = "GIF文件(*.gif)|*.gif";SaveFile.Title = "保存gif文件";break;case 3:SaveFile.Filter = "JPG文件(*.jpeg)|*.jpeg";SaveFile.Title = "保存jpg文件";break;case 4:SaveFile.Filter = "PNG文件(*.png)|*.png";SaveFile.Title = "保存png文件";break;case 5:SaveFile.Filter = "TIFF文件(*.TIFF)|*.TIFF";SaveFile.Title = "保存tiff文件";break;case 6:SaveFile.Filter = "PDF文件(*.PDF)|*.pdf";SaveFile.Title = "保存pdf文件";break;}if (txtResolution.Value > 1){ if (SaveFile.ShowDialog() == DialogResult.OK){//打開(kāi)保存文件對(duì)話框 并選擇路徑和文件名SaveFile.AddExtension = true;//設(shè)置為自動(dòng)補(bǔ)充文件擴(kuò)展名IExport pExport;switch (comboBox1.SelectedIndex)//根據(jù)掩碼文本框的索引值穿件不同的pExport實(shí)例并調(diào)用Export方法{case 0:pExport = new ExportBMPClass();Export(pExport, SaveFile.FileName);break;case 2:pExport = new ExportGIFClass();Export(pExport, SaveFile.FileName);break;case 3:pExport = new ExportJPEGClass();Export(pExport, SaveFile.FileName);break;case 4:pExport = new ExportPNGClass();Export(pExport, SaveFile.FileName);break;case 5:pExport = new ExportTIFFClass();Export(pExport, SaveFile.FileName);break;case 6:pExport = new ExportPDFClass();Export(pExport, SaveFile.FileName);break;}}}else {MessageBox.Show("請(qǐng)輸入有效數(shù)字~!", "提示", MessageBoxButtons.OK);}}//輸出文件public void Export(IExport pExport, string path){IActiveView pActiveView;IEnvelope pPixelBoundsEnv;int iOutputResolution;int iScreenResolution;int hdc;pActiveView = m_Map.ActiveView;pExport.ExportFileName = path;iScreenResolution = 96;iOutputResolution = 300;pExport.Resolution = iOutputResolution;tagRECT pExportFrame;pExportFrame = pActiveView.ExportFrame;tagRECT exportRECT;exportRECT.left = 0;exportRECT.top = 0;exportRECT.right = pActiveView.ExportFrame.right * (iOutputResolution / iScreenResolution);exportRECT.bottom = pActiveView.ExportFrame.bottom * (iOutputResolution / iScreenResolution);pPixelBoundsEnv = new EnvelopeClass();pPixelBoundsEnv.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);pExport.PixelBounds = pPixelBoundsEnv;hdc = pExport.StartExporting();pActiveView.Output(hdc, (int)pExport.Resolution, ref exportRECT, null, null);pExport.FinishExporting();pExport.Cleanup();MessageBox.Show("成功保存地圖~!", "保存", MessageBoxButtons.OK);}?
?
?
打印地圖
?
AxPageLayoutControl m_Map;//新建axPageLayoutControl變量,用于接收傳進(jìn)來(lái)的參數(shù)private PageSetupDialog pageSetup=new PageSetupDialog();PrintDocument PD = new PrintDocument();private PrintDialog print=new PrintDialog();public PrintMap(AxPageLayoutControl m_Map){InitializeComponent();this.Text = "打印地圖";this.m_Map = m_Map;copy(m_Map);// copyMap(m_Map,axPageLayoutControl1);m_Map.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);}/* public void copyMap(AxPageLayoutControl m_Map, AxPageLayoutControl axPageLayoutControl){ IObjectCopy objectCopy = new ObjectCopy();object tocopyMap = m_Map.ActiveView.GraphicsContainer;object copiedMap = objectCopy.Copy(tocopyMap);object toOverwiteMap = axPageLayoutControl1.ActiveView.FocusMap;objectCopy.Overwrite(copiedMap, ref toOverwiteMap);}*/ private void PrintMap_Load(object sender, EventArgs e){pageSetup = new PageSetupDialog();//初始化頁(yè)面設(shè)置對(duì)話框的頁(yè)面設(shè)置屬性為缺省設(shè)置pageSetup.PageSettings = new System.Drawing.Printing.PageSettings();//初始化頁(yè)面設(shè)置對(duì)話框的打印機(jī)屬性為缺省設(shè)置pageSetup.PrinterSettings = new System.Drawing.Printing.PrinterSettings();print = new PrintDialog();}private void copy(AxPageLayoutControl mainlayoutControl){IObjectCopy objectcopy = new ObjectCopyClass();object tocopymap = mainlayoutControl.ActiveView.GraphicsContainer; //獲取mapcontrol中的map 這個(gè)是原始的object copiedmap = objectcopy.Copy(tocopymap); //復(fù)制一份map,是一個(gè)副本object tooverwritemap = axPageLayoutControl1.ActiveView.GraphicsContainer; //IActiveView.FocusMap : The map that tools and controls act on. 控件和工具作用的地圖,大概是當(dāng)前地圖吧!!!objectcopy.Overwrite(copiedmap, ref tooverwritemap);System.Runtime.InteropServices.Marshal.ReleaseComObject(objectcopy);IGraphicsContainer mainGraphCon = tooverwritemap as IGraphicsContainer;mainGraphCon.Reset();IElement pElement = mainGraphCon.Next();IArray pArray = new ArrayClass();while (pElement != null){pArray.Add(pElement);pElement = mainGraphCon.Next();}int pElementCount = pArray.Count;IPageLayout PrintPageLayout = axPageLayoutControl1.PageLayout;IGraphicsContainer PrintGraphCon = PrintPageLayout as IGraphicsContainer;PrintGraphCon.Reset();IElement pPrintElement = PrintGraphCon.Next();while (pPrintElement != null){PrintGraphCon.DeleteElement(pPrintElement);pPrintElement = PrintGraphCon.Next();}for (int i = 0; i < pArray.Count; i++){PrintGraphCon.AddElement(pArray.get_Element(pElementCount - 1 - i) as IElement, 0);}axPageLayoutControl1.Refresh();}private void button1_Click(object sender, EventArgs e){//實(shí)例化打印設(shè)置窗口DialogResult result = pageSetup.ShowDialog();//設(shè)置打印文檔對(duì)象的打印機(jī)PD.PrinterSettings = pageSetup.PrinterSettings;//設(shè)置打印文檔對(duì)象的頁(yè)面設(shè)置為用戶在打印設(shè)置對(duì)話框中的設(shè)置PD.DefaultPageSettings = pageSetup.PageSettings;//頁(yè)面設(shè)置int i;IEnumerator paperSizes = pageSetup.PrinterSettings.PaperSizes.GetEnumerator();paperSizes.Reset();for (i = 0; i < pageSetup.PrinterSettings.PaperSizes.Count; ++i){paperSizes.MoveNext();if (((PaperSize)paperSizes.Current).Kind == PD.DefaultPageSettings.PaperSize.Kind){PD.DefaultPageSettings.PaperSize = ((PaperSize)paperSizes.Current);}}//初始化紙張和打印機(jī)IPaper paper = new PaperClass();IPrinter printer = new EmfPrinterClass();//關(guān)聯(lián)打印機(jī)對(duì)象和紙張對(duì)象 paper.Attach(pageSetup.PrinterSettings.GetHdevmode(pageSetup.PageSettings).ToInt32(), pageSetup.PrinterSettings.GetHdevnames().ToInt32());printer.Paper = paper;axPageLayoutControl1.Printer = printer;pageSetup.ShowDialog();}private void btnPrint_Click(object sender, EventArgs e){//顯示幫助按鈕 print.ShowHelp = true;print.Document = PD;//顯示打印窗口DialogResult result = print.ShowDialog();// 如果顯示成功,則打印.if (result == DialogResult.OK) PD.Print();Close();}} }比較綜合。截圖太多,就不放了。exe可運(yùn)行,放百度網(wǎng)盤(pán)了。一定結(jié)合百度云的窗體,運(yùn)行一下再去看代碼,最后的結(jié)課報(bào)告有完整的功能介紹和運(yùn)行結(jié)果截圖和測(cè)試,需要的可以留言或私信。
百度云鏈接:
鏈接:https://pan.baidu.com/s/1NTFdUBJGBO69SJ3CRUKWCQ 密碼:tqfp
總結(jié)
以上是生活随笔為你收集整理的VSC# ArcGIS 二次开发--出图工具:添加图名、比例尺、指北针、图例,导出图片或文件、打印地图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 基于cocos2d-x简易泡泡龙游戏二
- 下一篇: 【飞鱼科技】最新社招信息