??? 在ArcGIS的開發(fā)中,我們經(jīng)常需要將當(dāng)前地圖打印(或是轉(zhuǎn)出)到圖片文件中。將Map或Layout中的圖象轉(zhuǎn)出有兩種方法,一種為通過
IActiveView的OutPut函數(shù),另外一種是通過IExport接口來實(shí)現(xiàn)。第一種方法導(dǎo)出速度較快,實(shí)現(xiàn)也比較方便,但該方法對(duì)于圖片的行或
列數(shù)超過10000左右時(shí),導(dǎo)出經(jīng)常會(huì)失敗(具體原因未知),第二種方法導(dǎo)出速度較慢,但效果較好,且可以在導(dǎo)出過程中通過ITrackCancel來中
止導(dǎo)出操作。
????? 通過IActiveView的方式導(dǎo)出是通過創(chuàng)建Graphics對(duì)象來實(shí)現(xiàn),具體示例代碼如下:
- ///?<summary> ??
- ??
- ///?將Map上指定范圍(該范圍為規(guī)則區(qū)域)內(nèi)的內(nèi)容輸出到Image,注意,當(dāng)圖片的行數(shù)或列數(shù)超過10000左右時(shí),出現(xiàn)原因示知的失敗 ??
- ??
- ///?</summary> ??
- ??
- ///?<param?name="pMap">需轉(zhuǎn)出的MAP</param> ??
- ///?<param?name="outRect">輸出的圖片大小</param> ??
- ///?<param?name="pEnvelope">指定的輸出范圍(為Envelope類型)</param> ??
- ///?<returns>輸出的Image?具體需要保存為什么格式,可通過Image對(duì)象來實(shí)現(xiàn)</returns> ??
- public ? static ?Image?SaveCurrentToImage(IMap?pMap,?Size?outRect,?IEnvelope?pEnvelope) ??
- ?{ ??
- ?????? //賦值 ??
- ??????tagRECT?rect?=? new ?tagRECT(); ??
- ??????rect.left?=?rect.top?=? 0 ; ??
- ??????rect.right?=?outRect.Width; ??
- ??????rect.bottom?=?outRect.Height; ??
- ?????? try ??
- ??????{???????????????? ??
- ?????????? //轉(zhuǎn)換成activeView,若為ILayout,則將Layout轉(zhuǎn)換為IActiveView ??
- ??????????IActiveView?pActiveView?=?(IActiveView)pMap; ??
- ?????????? //?創(chuàng)建圖像,為24位色 ??
- ??????????Image?image?=? new ?Bitmap(outRect.Width,?outRect.Height);? //,?System.Drawing.Imaging.PixelFormat.Format24bppRgb); ??
- ??????????System.Drawing.Graphics?g?=?System.Drawing.Graphics.FromImage(image); ??
- ??
- ?????????? //?填充背景色(白色) ??
- ??????????g.FillRectangle(Brushes.White,? 0 ,? 0 ,?outRect.Width,?outRect.Height); ??
- ??
- ?????????? int ?dpi?=?( int )(outRect.Width?/?pEnvelope.Width); ??
- ??
- ??????????pActiveView.Output(g.GetHdc().ToInt32(),?dpi,?ref?rect,?pEnvelope,? null ); ??
- ??
- ??????????g.ReleaseHdc();???????????? ??
- ??
- ?????????? return ?image; ??
- ?????} ??
- ??
- ????? catch ?(Exception?excp) ??
- ?????{ ??
- ????????MessageBox.Show(excp.Message?+? "將當(dāng)前地圖轉(zhuǎn)出出錯(cuò),原因未知" ,? "出錯(cuò)提示" ,?MessageBoxButtons.OK,?MessageBoxIcon.Error); ??
- ??
- ?????????? return ? null ; ??
- ??????} ??
- ?}??
/// <summary> /// 將Map上指定范圍(該范圍為規(guī)則區(qū)域)內(nèi)的內(nèi)容輸出到Image,注意,當(dāng)圖片的行數(shù)或列數(shù)超過10000左右時(shí),出現(xiàn)原因示知的失敗 /// </summary> /// <param name="pMap">需轉(zhuǎn)出的MAP</param> /// <param name="outRect">輸出的圖片大小</param> /// <param name="pEnvelope">指定的輸出范圍(為Envelope類型)</param> /// <returns>輸出的Image 具體需要保存為什么格式,可通過Image對(duì)象來實(shí)現(xiàn)</returns> public static Image SaveCurrentToImage(IMap pMap, Size outRect, IEnvelope pEnvelope) { //賦值 tagRECT rect = new tagRECT(); rect.left = rect.top = 0; rect.right = outRect.Width; rect.bottom = outRect.Height; try { //轉(zhuǎn)換成activeView,若為ILayout,則將Layout轉(zhuǎn)換為IActiveView IActiveView pActiveView = (IActiveView)pMap; // 創(chuàng)建圖像,為24位色 Image image = new Bitmap(outRect.Width, outRect.Height); //, System.Drawing.Imaging.PixelFormat.Format24bppRgb); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); // 填充背景色(白色) g.FillRectangle(Brushes.White, 0, 0, outRect.Width, outRect.Height); int dpi = (int)(outRect.Width / pEnvelope.Width); pActiveView.Output(g.GetHdc().ToInt32(), dpi, ref rect, pEnvelope, null); g.ReleaseHdc(); return image; } catch (Exception excp) { MessageBox.Show(excp.Message + "將當(dāng)前地圖轉(zhuǎn)出出錯(cuò),原因未知", "出錯(cuò)提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return null; } }
?? 通過IExport接口實(shí)現(xiàn)的導(dǎo)出,也需要通過IActiveView的OutPut來實(shí)現(xiàn),但其轉(zhuǎn)出句柄為IExport的StartExporting函數(shù)返回的DC,具體示例代碼如下:
- //輸出當(dāng)前地圖至指定的文件???? ??
- public ? void ?ExportMapExtent(IActiveView?pView,?Size?outRect,string?outPath) ??
- {??????????? ??
- ???? try ??
- ????{ ??
- ???????? //參數(shù)檢查 ??
- ???????? if ?pView?==? null ?) ??
- ????????{ ??
- ???????????? throw ? new ?Exception( "輸入?yún)?shù)錯(cuò)誤,無法生成圖片文件!" ); ??
- ????????}?? ??
- ???????? //根據(jù)給定的文件擴(kuò)展名,來決定生成不同類型的對(duì)象 ??
- ????????ESRI.ArcGIS.Output.IExport?export?=? null ; ??
- ???????? if ?(outPath.EndsWith( ".jpg" )) ??
- ????????{ ??
- ????????????export?=? new ?ESRI.ArcGIS.Output.ExportJPEGClass(); ??
- ????????} ??
- ???????? else ? if ?(outPath.EndsWith( ".tiff" )) ??
- ????????{ ??
- ????????????export?=? new ?ESRI.ArcGIS.Output.ExportTIFFClass(); ??
- ????????} ??
- ???????? else ? if ?(outPath.EndsWith( ".bmp" )) ??
- ????????{ ??
- ????????????export?=? new ?ESRI.ArcGIS.Output.ExportBMPClass(); ??
- ????????} ??
- ???????? else ? if ?(outPath.EndsWith( ".emf" )) ??
- ????????{ ??
- ????????????export?=? new ?ESRI.ArcGIS.Output.ExportEMFClass(); ??
- ????????} ??
- ???????? else ? if ?(outPath.EndsWith( ".png" )) ??
- ????????{ ??
- ????????????export?=? new ?ESRI.ArcGIS.Output.ExportPNGClass(); ??
- ????????} ??
- ???????? else ? if ?(outPath.EndsWith( ".gif" )) ??
- ????????{ ??
- ????????????export?=? new ?ESRI.ArcGIS.Output.ExportGIFClass(); ??
- ????????} ??
- ??
- ????????export.ExportFileName?=?outPath; ??
- ????????IEnvelope?pEnvelope?=?pView.Extent; ??
- ???????? //導(dǎo)出參數(shù)??????????? ??
- ????????export.Resolution?=? 300 ; ??
- ????????tagRECT?exportRect?=? new ?tagRECT(); ??
- ????????exportRect.left?=?exportRect.top?=? 0 ; ??
- ????????exportRect.right?=?outRect.Width; ??
- ????????exportRect.bottom?=?( int )(exportRect.right?*?pEnvelope.Height?/?pEnvelope.Width); ??
- ????????ESRI.ArcGIS.Geometry.IEnvelope?envelope?=? new ?ESRI.ArcGIS.Geometry.EnvelopeClass(); ??
- ???????? //輸出范圍 ??
- ????????envelope.PutCoords(exportRect.left,?exportRect.top,?exportRect.right,?exportRect.bottom); ??
- ????????export.PixelBounds?=?envelope; ??
- ???????? //可用于取消操作 ??
- ????????ITrackCancel?pCancel?=? new ?CancelTrackerClass(); ??
- ????????export.TrackCancel?=?pCancel; ??
- ????????pCancel.Reset(); ??
- ???????? //點(diǎn)擊ESC鍵時(shí),中止轉(zhuǎn)出 ??
- ????????pCancel.CancelOnKeyPress?=? true ; ??
- ????????pCancel.CancelOnClick?=? false ; ??
- ????????pCancel.ProcessMessages?=? true ; ??
- ???????? //獲取handle ??
- ????????System.Int32?hDC?=?export.StartExporting(); ??
- ???????? //開始轉(zhuǎn)出 ??
- ????????pView.Output(hDC,?(System.Int16)export.Resolution,?ref?exportRect,?pEnvelope,?pCancel); ??
- ????????bool?bContinue?=?pCancel.Continue(); ??
- ???????? //捕獲是否繼續(xù) ??
- ???????? if ?(bContinue) ??
- ????????{?????????????????????????????? ??
- ????????????export.FinishExporting(); ??
- ????????????export.Cleanup(); ??
- ????????} ??
- ???????? else ??
- ????????{?????????????????? ??
- ????????????export.Cleanup(); ??
- ????????} ??
- ????????bContinue?=?pCancel.Continue();??????????????? ??
- ????} ??
- ???? catch ?(Exception?excep) ??
- ????{ ??
- ???????? //錯(cuò)誤信息提示 ??
- ????} ??
-
}?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
