c# SOE ArcEngine 无AxMapController可视化窗体导出图片

  /// 
    /// 生成图片实现类-20200923
    /// 
    class GenerateImagesImpl
    {

        [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
        protected static extern IntPtr GetDesktopWindow();
        /// 
        /// 得到系统窗口的句柄,传递给mxd用作激活窗口用途
        /// 
        /// 
        int GetWinhWnd()
        {
            int hWnd = GetDesktopWindow().ToInt32();
            return hWnd;
        }

        /// 
        /// 生成图片方法
        /// 
        /// 图层名称
        /// 
        public string GenerateImages(string nameLayer)
        {
            IFeatureWorkspace featureWorkspace = SDEOperation.ConnectAndOpenFeatureWorkspace();
            IFeatureLayer featureLayer = new FeatureLayer();
            featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass(nameLayer);//SDE.GHSJ_KJGH_YDXZKZXXGH_KG
            IMap map = new Map();
            map.AddLayer(featureLayer as ILayer);
            IMxdContents pMxdC = map as IMxdContents;
            IMapDocument pMapDocument = new MapDocumentClass();
            string pathMxd = "E:\\" + UuidHelper.NewUuidString() + ".mxd";//uuid是一个随机字符,可自定义
            pMapDocument.New(pathMxd);
            pMapDocument.ReplaceContents(pMxdC);
            pMapDocument.Save(true, true);
            pMapDocument.Close();
            pMapDocument.Open(pathMxd, "");
            pMapDocument.ActiveView.Activate(GetWinhWnd());//激活窗口
            ExportImage2(pMapDocument.ActiveView, pathMxd.Replace(".mxd", ".jpg"), 150);//导出图片
            pMapDocument.Close();
            File.Delete(pathMxd);
            CommonUtils.RelCommonObject(pMapDocument);
            CommonUtils.RelCommonObject(pMxdC);
            CommonUtils.RelCommonObject(map);
            CommonUtils.RelCommonObject(featureLayer);
            CommonUtils.RelCommonObject(featureWorkspace);
            return pathMxd.Replace(".mxd", ".jpg");
        }

        public static void ExportImage2(IActiveView pActiveView, string outPath, int pdi)
        {
            try
            {
                //参数检查  
                if (pActiveView == null)
                {
                    throw new Exception("输入参数错误,无法生成图片文件!");
                }
                //根据给定的文件扩展名,来决定生成不同类型的对象  
                ESRI.ArcGIS.Output.IExport export = null;
                if (outPath.EndsWith(".pdf"))
                {
                    export = new ESRI.ArcGIS.Output.ExportPDFClass();
                }
                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();
                }
                else if (outPath.EndsWith(".jpg"))
                {
                    export = new ESRI.ArcGIS.Output.ExportJPEGClass();
                }
                export.ExportFileName = outPath;
                IEnvelope pEnvelope = pActiveView.Extent;

                IEnvelope envelope = new EnvelopeClass();
                System.Int32 screenResolution = 96;
                System.Int32 outputResolution = 310;
                //System.Int32 screenResolution = 50;
                //System.Int32 outputResolution = 5;
                export.Resolution = outputResolution;
                tagRECT exportRect; // This is a structure
                exportRect.left = 0;
                exportRect.top = 0;
                exportRect.right = pActiveView.ExportFrame.right * (outputResolution / screenResolution);
                exportRect.bottom = pActiveView.ExportFrame.bottom * (outputResolution / screenResolution);

                //输出范围  
                envelope.PutCoords(exportRect.left, exportRect.top, exportRect.right, exportRect.bottom);
                export.PixelBounds = envelope;
                //可用于取消操作  
                ITrackCancel pCancel = new CancelTrackerClass();
                export.TrackCancel = pCancel;
                pCancel.Reset();
                //点击ESC键时,中止转出  
                pCancel.CancelOnKeyPress = true;
                pCancel.CancelOnClick = false;
                pCancel.ProcessMessages = true;
                //获取handle  
                System.Int32 hDC = export.StartExporting();
                //开始转出  
                pActiveView.Output(hDC, (System.Int16)export.Resolution, ref exportRect, null, null);
                bool bContinue = pCancel.Continue();
                //捕获是否继续  
                if (bContinue)
                {
                    export.FinishExporting();
                    export.Cleanup();///(1)
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(export);///(2)
                    GC.Collect();(3)三管齐下,强制回收内存垃圾
                }
                else
                {
                    export.Cleanup();///(1)
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(export);///(2)
                    GC.Collect();(3)三管齐下,强制回收内存垃圾
                }
                bContinue = pCancel.Continue();

            }
            catch (Exception ex)
            {

            }
        }

 

 QQ交流群:607330463 GIS开发技术最强交流群

你可能感兴趣的:(ArcServer,ArcEngine,GP,SOE)