ArcEngine 自定义范围输出为栅格图像(JPG/BMP)

时间:2024-02-22 18:39:43
/// <summary>

        /// 打印输出.该功能目前测试只适用于JPEG,BMP.格式

        /// </summary>

        /// <param name="pExport"></param>

        /// <param name="dResolution">分辨率</param>

        /// <param name="outputPath">输出路径</param>

        /// <param name="pVisibleBounds">自定义可见区域</param>

        private void FunExportImage(IExport pExport, double dResolution, string outputPath,IEnvelope pVisibleBounds)

        {

            IEnvelope pPixelBounds;

            ESRI.ArcGIS.Display.tagRECT outtagRECT;

            ESRI.ArcGIS.Display.tagRECT DisplayBounds = m_ActiveView.ExportFrame;

            double iScreenResolution;

            if (pExport == null)

            {

                MessageBox.Show("打印类型未指定");

                return;

            }



            if(pVisibleBounds!=null) //自定义框范围

            {

                IDisplayTransformation pDisplayTransformation = m_ActiveView.ScreenDisplay.DisplayTransformation;

                pDisplayTransformation.TransformRect(pVisibleBounds, ref DisplayBounds, 8);//8代表esriDisplayTransformEnum.esriTransformToDevice

            }



            if (pExport is IExportImage)

            {

                IExportImage pExportImage = pExport as IExportImage;

                pExportImage.ImageType = esriExportImageType.esriExportImageTypeTrueColor;

            }



            iScreenResolution = m_ActiveView.ScreenDisplay.DisplayTransformation.Resolution;

                      

            double l_resolution = Convert.ToDouble(domainResolution.Value);



            //=====================================================================

            //

            //方法一

            //

            //一厘米包含37.79524个像素

            //double dPixel = 37.79524;

            ////按照设置输入分辨率计算一厘米所含的像素数

            //double tempratio = l_resolution * dPixel / iScreenResolution;

            ////输出图形的高度

            //double tempbottom = (DisplayBounds.bottom - DisplayBounds.top) * tempratio;

            ////输出图形的宽度

            //double tempright = (DisplayBounds.right - DisplayBounds.left) * tempratio + 0.1 * tempratio;

            //======================================================================

            //

            //方法二:暂时可行.

            //

            double tempratio = l_resolution / iScreenResolution;

            double tempbottom = (DisplayBounds.bottom - DisplayBounds.top) * tempratio;

            double tempright = (DisplayBounds.right - DisplayBounds.left) * tempratio;

            //=====================================================================

            outtagRECT.left = 0;

            outtagRECT.top = 0;

            outtagRECT.bottom = Convert.ToInt32(Math.Truncate(tempbottom));

            outtagRECT.right = Convert.ToInt32(Math.Truncate(tempright));



            pPixelBounds = new EnvelopeClass();

            pPixelBounds.PutCoords(outtagRECT.left, outtagRECT.top, outtagRECT.right, outtagRECT.bottom);



            pExport.Resolution = dResolution;

            pExport.PixelBounds = pPixelBounds;

            pExport.ExportFileName = outputPath;



            try

            {

                ITrackCancel pTrackCancel = new TrackCancelClass();

                int hDC;

                hDC = pExport.StartExporting();



                if (m_frmProBar == null) //m_frmProBar为进度条窗体

                    FunProgressBar();

                m_frmProBar.Show();



                m_ActiveView.Output(hDC, (int)dResolution, ref outtagRECT, pVisibleBounds, pTrackCancel);

                pExport.FinishExporting();



                m_frmProBar.Hide();



                if (DialogResult.Yes == MessageBox.Show("出图成功! \n图片保存在" + txtOutPutPath.Text+"\n是否需要打开文件所在的目录?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information))

                {

                    System.Diagnostics.Process.Start("explorer.exe", txtOutPutPath.Text); 

                }

                pExport.Cleanup();

            }

            catch

            {

                MessageBox.Show("出图失败!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);

                //释放变量

                m_frmProBar = null;

                pExport = null;

                pVisibleBounds = null;

            }

            //释放变量

            pExport = null;

            pVisibleBounds = null;



        }