1. 程式人生 > 實用技巧 >arcgis engine指定範圍匯出螢幕圖片

arcgis engine指定範圍匯出螢幕圖片

地理座標下,所以要轉換為度。

arcgis engine 10.2.2 +vs2010

        [DllImport("GDI32.dll")]
        public static extern int GetDeviceCaps(int hdc, int nIndex);

        /* User32 delegates to getDC and ReleaseDC */
        [DllImport("User32.dll")]
        public static extern int GetDC(int hWnd);

        [DllImport(
"User32.dll")] public static extern int ReleaseDC(int hWnd, int hDC); [DllImport("user32.dll", SetLastError = true)] static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, uint fWinIni); /// <summary> /// 地圖距離轉螢幕畫素數,不同縮放比例尺下同樣距離對應畫素數不同的,有特殊需要時設定sMapBLC
/// </summary> /// <param name="pAV">The p AV.</param> /// <param name="dMapUnits">地圖距離</param> /// <returns></returns> public static double ConvertMapUnitsToPixels(IActiveView pAV, double dMapUnits) { IDisplayTransformation pDT
= pAV.ScreenDisplay.DisplayTransformation; tagRECT pDeviceFrame = pDT.get_DeviceFrame(); int iDeviceLeft = pDeviceFrame.left; int iDeviceRight = pDeviceFrame.right; int iPixelExtent = iDeviceRight - iDeviceLeft; double dRealWorldExtent = pAV.Extent.Width; double dSizeOfEachPixel = dRealWorldExtent / iPixelExtent; return dMapUnits / dSizeOfEachPixel; } /// <summary> /// 指定範圍裁剪圖片 /// </summary> /// <param name="pMap">裁圖地圖視窗</param> /// <param name="pExtent">指定裁圖範圍</param> /// <param name="strPicFile">輸出檔名稱</param> /// <param name="iOutResolution">輸出DPI</param> public static void ClipMap2Pic(IActiveView pAV, IEnvelope pExtent, string strPicFile, int iOutResolution) { if (pAV == null) return; if (strPicFile == string.Empty) return; string ext = strPicFile.Substring(strPicFile.Length - 4).ToUpper(); IExport pExport = null; if (ext == ".TIF") //根據檔名後4位判斷輸出型別 pExport = new ExportTIFFClass(); else if (ext == ".EMF") pExport = new ExportEMFClass(); else if (ext == ".BMP") pExport = new ExportBMPClass(); else if (ext == ".GIF") pExport = new ExportGIFClass(); else if (ext == ".PDF") pExport = new ExportPDFClass(); else if (ext == ".PNG") pExport = new ExportPNGClass(); else if (ext == ".SVG") pExport = new ExportSVGClass(); else if (strPicFile.Substring(strPicFile.Length - 4).ToUpper() == ".AI") pExport = new ExportAIClass(); else if (ext == ".EPS") pExport = new ExportPSClass(); else pExport = new ExportJPEGClass(); IDisplayTransformation pDT = pAV.ScreenDisplay.DisplayTransformation; IOutputRasterSettings docOutputRasterSettings = pDT as IOutputRasterSettings; int iPrevOutputImageQuality = docOutputRasterSettings.ResampleRatio; docOutputRasterSettings.ResampleRatio = 1; /* Get the device context of the screen */ long tmpDC = GetDC(0); /* Get the screen resolution. */ int iScreenResolution = GetDeviceCaps((int)tmpDC, 88); //88 is the win32 const for Logical pixels/inch in X) /* release the DC. */ ReleaseDC(0, (int)tmpDC); // 根據傳入的pExtent確定裁圖的畫布大小(出圖比例尺下裁圖範圍的畫素高和寬) tagRECT ptagTmp; ptagTmp.left = 0; ptagTmp.top = 0; ptagTmp.right = (int)Math.Truncate(ConvertMapUnitsToPixels(pAV, pExtent.Width) * iOutResolution / iScreenResolution); //把地圖距離轉為螢幕畫素個數 ptagTmp.bottom = (int)Math.Truncate(ConvertMapUnitsToPixels(pAV, pExtent.Height) * iOutResolution / iScreenResolution); IEnvelope pEnvNew = new EnvelopeClass(); pEnvNew.PutCoords(ptagTmp.left, ptagTmp.top, ptagTmp.right, ptagTmp.bottom); //裁圖的畫布大小 pExport.ExportFileName = strPicFile; pExport.Resolution = iOutResolution; pExport.PixelBounds = pEnvNew; if (ext == ".TIF") { (pExport as IExportTIFF).GeoTiff = true;//包含tif檔案座標資訊,這2句是必須的 (pExport as IWorldFileSettings).MapExtent = pExtent; (pExport as IExportTIFF).CompressionType = esriTIFFCompression.esriTIFFCompressionJPEG; } if (ext == ".PDF") { (pExport as IExportPDF).Compressed = true; (pExport as IExportPDF).EmbedFonts = true; (pExport as IExportPDF).ImageCompression = esriExportImageCompression.esriExportImageCompressionNone; } int ihdc = pExport.StartExporting(); pAV.Output(ihdc, iOutResolution, ref ptagTmp, pExtent, null); pExport.FinishExporting(); pExport.Cleanup(); (pDT as IOutputRasterSettings).ResampleRatio = iPrevOutputImageQuality; } void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e) { if (isExportMode) { IEnvelope bounds = new EnvelopeClass(); double dis = 100;//100米 IUnitConverter convert = new UnitConverterClass(); double degree = convert.ConvertUnits(dis, esriUnits.esriMeters, esriUnits.esriDecimalDegrees);//轉換為度 bounds.PutCoords(e.mapX - degree, e.mapY + degree, e.mapX + degree, e.mapY - degree); string file = System.IO.Path.Combine(@"D:\test22\image", DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"); ClipMap2Pic(this.axMapControl1.ActiveView, bounds, file, 96); } }