// ArcGIS Snippet Title:
        // Create JPEG from ActiveView
        //
        // Long Description:
        // Creates a .jpg (JPEG) file from IActiveView. Default values of 96 DPI are used for the image creation.
        //
        // Add the following references to the project:
        // ESRI.ArcGIS.Carto
        // ESRI.ArcGIS.Display
        // ESRI.ArcGIS.Geometry
        // ESRI.ArcGIS.Output
        // ESRI.ArcGIS.System
        //
        // Intended ArcGIS Products for this snippet:
        // ArcGIS Desktop (ArcEditor, ArcInfo, ArcView)
        // ArcGIS Engine
        // ArcGIS Server
        //
        // Applicable ArcGIS Product Versions:
        // 9.2
        // 9.3
        // 9.3.1
        // 10.0
        //
        // Required ArcGIS Extensions:
        // (NONE)
        //
        // Notes:
        // This snippet is intended to be inserted at the base level of a Class.
        // It is not intended to be nested within an existing Method.
        //

        ///<summary>Creates a .jpg (JPEG) file from IActiveView. Default values of 96 DPI are used for the image creation.</summary>
        ///
        ///<param name="activeView">An IActiveView interface</param>
        ///<param name="pathFileName">A System.String that the path and filename of the JPEG you want to create. Example: "C:\temp\test.jpg"</param>
        ///
        ///<returns>A System.Boolean indicating the success</returns>
        ///
        ///<remarks></remarks>
        private bool CreateJPEGFromActiveView(ESRI.ArcGIS.Carto.IActiveView activeView, System.String pathFileName)
        {
            //parameter check
            if (activeView == null || !(pathFileName.EndsWith(".jpg")))
            {
                return(false);
            }
            ESRI.ArcGIS.Output.IExport export = new ESRI.ArcGIS.Output.ExportJPEGClass();
            export.ExportFileName = pathFileName;

            // Microsoft Windows default DPI resolution
            export.Resolution = 300;
            ESRI.ArcGIS.esriSystem.tagRECT exportRECT = activeView.ExportFrame;
            ESRI.ArcGIS.Geometry.IEnvelope envelope   = new ESRI.ArcGIS.Geometry.EnvelopeClass();
            envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
            export.PixelBounds = envelope;
            System.Int32 hDC = export.StartExporting();
            activeView.Output(hDC, (System.Int16)export.Resolution, ref exportRECT, null, null);

            // Finish writing the export file and cleanup any intermediate files
            export.FinishExporting();
            export.Cleanup();

            MessageBox.Show("已将地图导出为jpg格式图片");
            return(true);
        }
        public static string CreateJPEGFromActiveView(IRaster pRaster,IEnvelope pEnv,string outurl, System.String pathFileName)
        {
            //创建rasterlayer
            IRasterLayer pRasterLayer = new RasterLayerClass();

            //获取mapServer中所有的图层
            IMap pMap = new MapClass();
            pRasterLayer.CreateFromRaster(pRaster);
            pMap.AddLayer(pRasterLayer as ILayer);

            IActiveView activeView = pMap as IActiveView;
            activeView.Extent = pEnv;
            //parameter check
            if (activeView == null)
            {
                return null;
            }
            string imageName = System.DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "") + ".png";
            pathFileName = System.IO.Path.Combine(pathFileName, imageName);

            ESRI.ArcGIS.Output.IExport export = new ESRI.ArcGIS.Output.ExportPNGClass();
            export.ExportFileName = pathFileName;

            // Microsoft Windows default DPI resolution

            ESRI.ArcGIS.esriSystem.tagRECT exportRECT = new ESRI.ArcGIS.esriSystem.tagRECT();
            exportRECT.top = 0;
            exportRECT.left = 0;
            exportRECT.right = 800;
            exportRECT.bottom = 600;
            ESRI.ArcGIS.Geometry.IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass();
            envelope.PutCoords(exportRECT.top,exportRECT.left,exportRECT.right,exportRECT.bottom);
            export.PixelBounds = envelope;
            System.Int32 hDC = export.StartExporting();
            activeView.Output(hDC, (System.Int16)export.Resolution, ref exportRECT, null, null);

            // Finish writing the export file and cleanup any intermediate files
            export.FinishExporting();
            export.Cleanup();

            return outurl + "/" + imageName;
        }
        //使用IActiveView.Output方法输出
        //Width和Hight用于设置输出的图片尺寸,默认为0则使用ActiveView.ExportFrame
        //VisibleBounds用于设置待输出的地图范围,默认为null则使用ActiveView.Extent
        private void ActiveViewOutput(IActiveView docActiveView, int iOutputResolution, int Width, int Height, IEnvelope VisibleBounds, string sExportFileName, IExport docExport)
        {
            docExport.ExportFileName = sExportFileName;
            if (iOutputResolution <= 0) iOutputResolution = 96;
            if (Width <= 0 || Height <= 0)
            {
            Width = docActiveView.ExportFrame.right;
            Height = docActiveView.ExportFrame.bottom;
            if (Width == 0)
                Width = 1024;
            if (Height == 0)
                Height = 768;
            Width = (Width * iOutputResolution) / 96;
            Height = (Height * iOutputResolution) / 96;
            }
            docExport.Resolution = iOutputResolution;
            ESRI.ArcGIS.esriSystem.tagRECT exportRECT = new ESRI.ArcGIS.esriSystem.tagRECT();
            exportRECT.left = 0;
            exportRECT.top = 0;
            exportRECT.right = Width;
            exportRECT.bottom = Height;

            IEnvelope envelope = new EnvelopeClass();
            envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
            docExport.PixelBounds = envelope;

            System.Int32 hDC = docExport.StartExporting();
            docActiveView.Output(hDC, iOutputResolution, ref exportRECT, VisibleBounds, null);
            docExport.FinishExporting();
            docExport.Cleanup();
        }
Beispiel #4
0
        //Function: Export map
        //Date: 2019/4/3
        public void ExportMap(int pOutputResolution, string pExportFileName, bool pWriteWorldFile, double pWidth, double pHeight)
        {
            IActiveView        docActiveView;
            IExport            docExport;
            IPrintAndExport    docPrintExport;
            IWorldFileSettings pWorldFile = null;

            ESRI.ArcGIS.esriSystem.tagRECT userRECT = new ESRI.ArcGIS.esriSystem.tagRECT();
            IEnvelope pEnv = new EnvelopeClass();

            string pFileType;
            int    pFileNameLength = pExportFileName.Length;

            if (pFileNameLength > 3)
            {
                pFileType = pExportFileName.Substring(pFileNameLength - 3, 3);
            }
            else
            {
                pFileType = pExportFileName;
            }

            switch (pFileType)
            {
            case "jpg":
                docExport = new ExportJPEGClass();
                break;

            case "png":
                docExport = new ExportPNGClass();
                break;

            case "tif":
                docExport = new ExportTIFFClass();
                break;

            default:
                docExport = new ExportJPEGClass();
                break;
            }

            if (miLayoutView.Checked)
            {
                docActiveView = axPageLayoutControl1.ActiveView;
            }
            else
            {
                docActiveView = axMapControl1.ActiveView;
            }

            pEnv                       = docActiveView.Extent;
            pWorldFile                 = (IWorldFileSettings)docExport;
            pWorldFile.MapExtent       = pEnv;
            pWorldFile.OutputWorldFile = pWriteWorldFile;

            userRECT.left   = 0;
            userRECT.top    = 0;
            userRECT.right  = Convert.ToInt32(pWidth);
            userRECT.bottom = Convert.ToInt32(pHeight);

            IEnvelope pDriverBounds = new EnvelopeClass();

            pDriverBounds.PutCoords(userRECT.top, userRECT.bottom, userRECT.right, userRECT.top);
            docExport.PixelBounds = pDriverBounds;

            docPrintExport = new PrintAndExportClass();

            docExport.ExportFileName = pExportFileName;
            docPrintExport.Export(docActiveView, docExport, pOutputResolution, true, null);
        }
Beispiel #5
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(textBoxFileName.Text.ToString()) == true)
            {
                MessageBox.Show("该文件已经存在,请重新命名!");
                textBoxFileName.SelectAll();
            }
            else
            {
                IExport            pExport    = null;
                IWorldFileSettings pWorldFile = null;
                IExportImage       pExportType;
                IEnvelope          pDriverBounds = null;

                ESRI.ArcGIS.esriSystem.tagRECT userRECT = new ESRI.ArcGIS.esriSystem.tagRECT();
                IEnvelope pEnv = new EnvelopeClass();

                string   FilePath    = this.m_strFileName;
                string[] strFileName = FilePath.Split('.');
                string   strFileType = strFileName[1];
                switch (strFileType)
                {
                case "jpg":
                    pExport = new ExportJPEGClass();
                    break;

                case "bmp":
                    pExport = new ExportBMPClass();
                    break;

                case "gif":
                    pExport = new ExportGIFClass();
                    break;

                case "tif":
                    pExport = new ExportTIFFClass();
                    break;

                case "png":
                    pExport = new ExportPNGClass();
                    break;

                case "emf":
                    pExport = new ExportEMFClass();
                    break;

                case "pdf":
                    pExport = new ExportPDFClass();
                    break;

                case ".ai":
                    pExport = new ExportAIClass();
                    break;

                case "svg":
                    pExport = new ExportSVGClass();
                    break;

                default:
                    pExport = new ExportJPEGClass();
                    break;
                }

                pExport.ExportFileName = this.m_strFileName;
                pExport.Resolution     = Convert.ToInt32(numUDresolution.Value);
                pExportType            = pExport as IExportImage;
                pExportType.ImageType  = esriExportImageType.esriExportImageTypeTrueColor;
                pEnv                       = m_pageLayoutControl.ActiveView.Extent;
                pWorldFile                 = (IWorldFileSettings)pExport;
                pWorldFile.MapExtent       = pEnv;
                pWorldFile.OutputWorldFile = false;
                userRECT.top               = 0;
                userRECT.left              = 0;
                userRECT.right             = Convert.ToInt32(txtBoxWidth.Text);
                userRECT.bottom            = Convert.ToInt32(txtBoxHeight.Text);
                pDriverBounds              = new EnvelopeClass();
                pDriverBounds.PutCoords(userRECT.top, userRECT.bottom, userRECT.right, userRECT.top);
                pExport.PixelBounds = pDriverBounds;
                ITrackCancel pTrackCancel = new TrackCancelClass();
                m_pageLayoutControl.ActiveView.Output(pExport.StartExporting(), Convert.ToInt32(numUDresolution.Value), ref userRECT, m_pageLayoutControl.ActiveView.Extent, pTrackCancel);
                pExport.FinishExporting();
                MessageBox.Show("打印图片保存成功!", "保存", MessageBoxButtons.OK);
                this.Close();
            }
        }