码迷,mamicode.com
首页 > 其他好文 > 详细

arcgis engine指定范围导出屏幕图片

时间:2020-11-23 12:49:37      阅读:30      评论:0      收藏:0      [点我收藏+]

标签:top   splay   gif   cer   length   substring   image   time   scl   

地理坐标下,所以要转换为度。

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);
            }
        }

 

arcgis engine指定范围导出屏幕图片

标签:top   splay   gif   cer   length   substring   image   time   scl   

原文地址:https://www.cnblogs.com/yansc/p/14006344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!