导出mxd专题图到图片

28 篇文章 0 订阅
21 篇文章 0 订阅

之前写了一些xmd文件导出图片的代码,但多多少少有一些bug,如范围、网格线、图例的显示bug问题。下面是比较全面的导出代码,亲测有效。

直接上代码:

using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Controls;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;

namespace DView.DotNet.Thematic
{
    public class MapToPicture
    {


        #region 导出JPEG函数及常量
        //导出JPEG函数及常量
        [DllImport("GDI32.dll")]
        public static extern int GetDeviceCaps(int hdc, int nIndex);

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

        const uint SPI_GETFONTSMOOTHING = 74;
        const uint SPI_SETFONTSMOOTHING = 75;
        const uint SPIF_UPDATEINIFILE = 0x1;

        #endregion

        public MapToPicture()
        {
        }

        #region 导出当前视图图片
        /// <summary>
        /// 导出当前视图图片
        /// </summary>
        /// <param name="iOutputResolution">分辨率</param>
        /// <param name="lResampleRatio">输出图像质量</param>
        /// <param name="ExportType">输出类型</param>
        /// <param name="sOutputDir">输出目录</param>
        /// <param name="bClipToGraphicsExtent">是否剪切为graphic</param>
        public static void ExportActiveViewParameterized(IActiveView sActiveView, long iOutputResolution, long lResampleRatio, string ExportType, string outPathStr, Boolean bClipToGraphicsExtent)
        {
            IActiveView docActiveView = sActiveView;// m_hookHelper.ActiveView;
            IExport docExport;
            long iPrevOutputImageQuality;
            IOutputRasterSettings docOutputRasterSettings;
            IEnvelope PixelBoundsEnv;
            tagRECT exportRECT;
            tagRECT DisplayBounds;
            IDisplayTransformation docDisplayTransformation;
            IPageLayout docPageLayout;
            IEnvelope docMapExtEnv;
            long hdc;
            long tmpDC;

            long iScreenResolution;
            bool bReenable = false;

            IEnvelope docGraphicsExtentEnv;
            IUnitConverter pUnitConvertor;

            if (GetFontSmoothing())
            {
                bReenable = true;
                DisableFontSmoothing();
                if (GetFontSmoothing())
                {
                    return;
                }
            }
            if (ExportType == "PDF")
            {
                docExport = new ExportPDFClass();
            }
            else if (ExportType == "EPS")
            {
                docExport = new ExportPSClass();
            }
            else if (ExportType == "AI")
            {
                docExport = new ExportAIClass();
            }
            else if (ExportType == "BMP")
            {

                docExport = new ExportBMPClass();
            }
            else if (ExportType == "TIF")
            {
                docExport = new ExportTIFFClass();
            }
            else if (ExportType == "SVG")
            {
                docExport = new ExportSVGClass();
            }
            else if (ExportType == "PNG")
            {
                docExport = new ExportPNGClass();
            }
            else if (ExportType == "GIF")
            {
                docExport = new ExportGIFClass();
            }
            else if (ExportType == "EMF")
            {
                docExport = new ExportEMFClass();
            }
            else if (ExportType == "JPG")
            {
                docExport = new ExportJPEGClass();
            }
            else
            {
                //DevExpress.XtraEditors.XtraMessageBox.Show("Unsupported export type " + ExportType + ", defaulting to EMF.");
                ExportType = "EMF";
                docExport = new ExportEMFClass();
            }
            docOutputRasterSettings = docActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
            iPrevOutputImageQuality = docOutputRasterSettings.ResampleRatio;
            if (docExport is IExportImage)
            {
                SetOutputQuality(docActiveView, 1);
            }
            else
            {

                SetOutputQuality(docActiveView, lResampleRatio);
            }
            //string sNameRoot = strPicName;
            if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(outPathStr)))
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(outPathStr));
            }
            docExport.ExportFileName = outPathStr;// sOutputDir +@"\"+ sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0];
            tmpDC = GetDC(0);
            iScreenResolution = GetDeviceCaps((int)tmpDC, 88);
            ReleaseDC(0, (int)tmpDC);
            docExport.Resolution = iOutputResolution;
            if (docActiveView is IPageLayout)
            {
                DisplayBounds = docActiveView.ExportFrame;
                docGraphicsExtentEnv = GetGraphicsExtent(docActiveView);
            }
            else
            {
                docDisplayTransformation = docActiveView.ScreenDisplay.DisplayTransformation;
                DisplayBounds = docDisplayTransformation.get_DeviceFrame();
            }

            PixelBoundsEnv = new Envelope() as IEnvelope;

            if (bClipToGraphicsExtent && (docActiveView is IPageLayout))
            {
                docGraphicsExtentEnv = GetGraphicsExtent(docActiveView);
                docPageLayout = docActiveView as PageLayout;
                pUnitConvertor = new UnitConverter();

                PixelBoundsEnv.XMin = 0;
                PixelBoundsEnv.YMin = 0;
                PixelBoundsEnv.XMax = pUnitConvertor.ConvertUnits(docGraphicsExtentEnv.XMax, docPageLayout.Page.Units, esriUnits.esriInches) * docExport.Resolution - pUnitConvertor.ConvertUnits(docGraphicsExtentEnv.XMin, docPageLayout.Page.Units, esriUnits.esriInches) * docExport.Resolution;
                PixelBoundsEnv.YMax = pUnitConvertor.ConvertUnits(docGraphicsExtentEnv.YMax, docPageLayout.Page.Units, esriUnits.esriInches) * docExport.Resolution - pUnitConvertor.ConvertUnits(docGraphicsExtentEnv.YMin, docPageLayout.Page.Units, esriUnits.esriInches) * docExport.Resolution;

                exportRECT.bottom = (int)(PixelBoundsEnv.YMax) + 1;
                exportRECT.left = (int)(PixelBoundsEnv.XMin);
                exportRECT.top = (int)(PixelBoundsEnv.YMin);
                exportRECT.right = (int)(PixelBoundsEnv.XMax) + 1;

                docMapExtEnv = docGraphicsExtentEnv;
            }
            else
            {
                double tempratio = (Convert.ToDouble(iOutputResolution)) / iScreenResolution;
                double tempbottom = DisplayBounds.bottom * tempratio;
                double tempright = DisplayBounds.right * tempratio;

                exportRECT.bottom = (int)Math.Truncate(tempbottom);
                exportRECT.left = 0;
                exportRECT.top = 0;
                exportRECT.right = (int)Math.Truncate(tempright);

                PixelBoundsEnv.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
                docMapExtEnv = null;
            }

            docExport.PixelBounds = PixelBoundsEnv;
            hdc = docExport.StartExporting();
            docActiveView.Output((int)hdc, (int)docExport.Resolution, ref exportRECT, docMapExtEnv, null);
            docExport.FinishExporting();
            docExport.Cleanup();
            //DevExpress.XtraEditors.XtraMessageBox.Show("成功导出地图到" + sOutputDir + sNameRoot + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0] + ".", "导出地图");
            SetOutputQuality(docActiveView, iPrevOutputImageQuality);
            if (bReenable)
            {
                EnableFontSmoothing();
                bReenable = false;
                if (!GetFontSmoothing())
                {
                    //DevExpress.XtraEditors.XtraMessageBox.Show("Unable to reenable Font Smoothing", "Font Smoothing error");
                }
            }
            docMapExtEnv = null;
            PixelBoundsEnv = null;
        }

        private static Boolean GetFontSmoothing()
        {
            bool iResult;
            int pv = 0;
            iResult = SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, ref pv, 0);
            if (pv > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private static void SetOutputQuality(IActiveView docActiveView, long iResampleRatio)
        {
            IGraphicsContainer oiqGraphicsContainer;
            IElement oiqElement;
            IOutputRasterSettings docOutputRasterSettings;
            IMapFrame docMapFrame;
            IActiveView TmpActiveView;

            if (docActiveView is IMap)
            {
                docOutputRasterSettings = docActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
                docOutputRasterSettings.ResampleRatio = (int)iResampleRatio;
            }
            else if (docActiveView is IPageLayout)
            {
                docOutputRasterSettings = docActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
                docOutputRasterSettings.ResampleRatio = (int)iResampleRatio;
                oiqGraphicsContainer = docActiveView as IGraphicsContainer;
                oiqGraphicsContainer.Reset();

                oiqElement = oiqGraphicsContainer.Next();
                while (oiqElement != null)
                {
                    if (oiqElement is IMapFrame)
                    {
                        docMapFrame = oiqElement as IMapFrame;
                        TmpActiveView = docMapFrame.Map as IActiveView;
                        docOutputRasterSettings = TmpActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
                        docOutputRasterSettings.ResampleRatio = (int)iResampleRatio;
                    }
                    oiqElement = oiqGraphicsContainer.Next();
                }
                docMapFrame = null;
                oiqGraphicsContainer = null;
                TmpActiveView = null;
            }
            docOutputRasterSettings = null;
        }

        private static void DisableFontSmoothing()
        {
            bool iResult;
            int pv = 0;
            iResult = SystemParametersInfo(SPI_SETFONTSMOOTHING, 0, ref pv, SPIF_UPDATEINIFILE);
        }

        private static IEnvelope GetGraphicsExtent(IActiveView docActiveView)
        {
            IEnvelope GraphicsBounds;
            IEnvelope GraphicsEnvelope;
            IGraphicsContainer oiqGraphicsContainer;
            IPageLayout docPageLayout;
            IDisplay GraphicsDisplay;
            IElement oiqElement;
            GraphicsBounds = new EnvelopeClass();
            GraphicsEnvelope = new EnvelopeClass();
            docPageLayout = docActiveView as IPageLayout;
            GraphicsDisplay = docActiveView.ScreenDisplay;
            oiqGraphicsContainer = docActiveView as IGraphicsContainer;
            oiqGraphicsContainer.Reset();

            oiqElement = oiqGraphicsContainer.Next();
            while (oiqElement != null)
            {
                oiqElement.QueryBounds(GraphicsDisplay, GraphicsEnvelope);
                GraphicsBounds.Union(GraphicsEnvelope);
                oiqElement = oiqGraphicsContainer.Next();
            }
            return GraphicsBounds;
        }

        private static void EnableFontSmoothing()
        {
            bool iResult;
            int pv = 0;
            iResult = SystemParametersInfo(SPI_SETFONTSMOOTHING, 1, ref pv, SPIF_UPDATEINIFILE);

        }
        #endregion

    }
}

这里再补上快速设置mxd数据源的代码

 IWorkspace workspace = // 这里获取自己的空做空间,如sde、gdb等;
 ThematicDataSourse dataSourse = new ThematicDataSourse();
 dataSourse.DataSourceUpdateFunction(workspace, ref mapDocument);



### 回答1: 要批量出专题图的mxd文件,可以使用Python编程语言以自动化的方式实现。以下是一种可能的实现方法: 1. 首先,你需要安装ArcPy库,并且确保你的Python环境已经设置好。 2. 在编写Python脚本之前,先确保你有一批用于生成专题图的mxd文件。可以将这些mxd文件放在一个文件夹中,以便后续批量处理。 3. 在Python脚本中,导入ArcPy库,并设置工作空间到包含你的mxd文件的文件夹。例如: ```python import arcpy arcpy.env.workspace = r"C:\path\to\your\mxd\folder" ``` 4. 使用arcpy.ListFiles函数获取该文件夹中的所有mxd文件的路径。例如: ```python mxd_files = arcpy.ListFiles("*.mxd") ``` 5. 使用for循环遍历这些mxd文件,并打开每个mxd文件。对于每个mxd文件,你可以执行一系列的操作,例如更改数据源路径、修改图层样式和符号、添加图例和文本等。 6. 如果你要将每个mxd文件保存为专题图,可以使用arcpy.mapping.ExportToPNG或arcpy.mapping.ExportToJPEG函数将其导出为PNG或JPEG格式的文件。例如: ```python arcpy.mapping.ExportToPNG(mxd_file, r"C:\path\to\output\folder\output.png") ``` 7. 最后,关闭mxd文件,以便释放资源,并继续处理下一个mxd文件。 通过以上步骤,你可以编写一个Python脚本,批量处理mxd文件生成专题图,并将其保存到指定的输出文件夹中。这样可以大大提高工作效率,尤其在需要生成大量专题图的情况下。 ### 回答2: 要批量制作专题图,可以使用Python编写脚本来实现。下面是一种可能的实现方式: 首先,需要导入`arcpy`模块,该模块提供了Python操作ArcGIS地理数据和地理信息系统的功能。 接下来,可以使用`arcpy.ListFiles()`函数列出指定目录下的所有mxd文件,可根据需要使用`for`循环进行逐个处理。例如,可以使用以下代码片段获取指定目录下的所有mxd文件: ```python import arcpy import os mxd_folder = r"C:\path\to\mxd\folder" mxd_files = arcpy.ListFiles("*.mxd") for mxd_file in mxd_files: mxd_path = os.path.join(mxd_folder, mxd_file) # 在这里进行专题图的处理 ``` 在每个循环迭代中,可以使用`arcpy.mapping.MapDocument()`函数打开mxd文件,并通过操作`arcpy.mapping`模块中的类和函数来对mxd进行操作。例如,可以使用以下代码片段将每个mxd文件另存为专题图: ```python import arcpy import os mxd_folder = r"C:\path\to\mxd\folder" output_folder = r"C:\path\to\output\folder" mxd_files = arcpy.ListFiles("*.mxd") for mxd_file in mxd_files: mxd_path = os.path.join(mxd_folder, mxd_file) mxd = arcpy.mapping.MapDocument(mxd_path) # 进行专题图的操作,例如更改图层样式、添加标注等 output_path = os.path.join(output_folder, mxd_file.replace(".mxd", ".pdf")) arcpy.mapping.ExportToPDF(mxd, output_path) mxd.saveACopy(output_path) del mxd ``` 这段代码将每个mxd文件保存为PDF格式,并保存到指定的输出文件夹中。可以根据需要修改输出格式和路径。 总之,以上是一个简单的示例,展示了如何使用Python批量制作专题图。具体的处理方式和操作根据需求和实际情况进行调整和扩展。 ### 回答3: 要实现Python批量出专题图(mxd文件),可以使用ArcPy模块来完成。步骤如下: 1. 首先,导入ArcPy模块: ```python import arcpy ``` 2. 然后,使用arcpy.ListFiles函数获取mxd文件的路径列表: ```python mxd_files = arcpy.ListFiles("*.mxd") ``` 3. 接下来,使用for循环遍历mxd文件列表,并使用arcpy.mapping.MapDocument函数加载每个mxd文件: ```python for mxd_file in mxd_files: mxd = arcpy.mapping.MapDocument(mxd_file) ``` 4. 根据需要,可以设定专题图的输出路径和文件名: ```python output_path = "output_folder_path" output_name = "output_name.png" output_file = output_path + "\\" + output_name ``` 5. 如果需要修改图层属性或添加其他要素,可以使用arcpy.mapping模块的相关函数进行修改: ```python layers = arcpy.mapping.ListLayers(mxd) for layer in layers: # 修改图层属性或添加其他要素的代码 ``` 6. 最后,使用arcpy.mapping.ExportToPNG函数将专题导出为PNG格式: ```python arcpy.mapping.ExportToPNG(mxd, output_file) ``` 通过以上步骤,就可以实现使用Python批量出专题图(mxd文件)。可以根据实际需求修改代码,例如修改导出格式、设置输出路径等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值