Exemple #1
0
        /// <summary>
        /// 输出图片
        /// </summary>
        /// <param name="activeView"></param>
        /// <param name="filename"></param>
        /// <param name="format"></param>
        /// <param name="resolution"></param>
        public static void ExportRasterFile(IActiveView activeView, string filename, string format, double resolution)
        {
            if (activeView == null || filename.Length == 0)
            {
                return;
            }

            double screenResolution = resolution;
            double outputResolution = resolution;

            IExport export = null;

            switch (format)
            {
            case "PDF":
                export = new ExportPDFClass();

                IExportVectorOptions exportVectorOptions = export as IExportVectorOptions;
                exportVectorOptions.PolygonizeMarkers = true;

                IExportPDF exportPDF = export as IExportPDF;
                exportPDF.EmbedFonts = true;
                break;

            case "BMP":
                export = new ExportBMPClass();
                break;

            case "JPG":
                export = new ExportJPEGClass();
                if (filename.IndexOf("彩信") != -1)
                {
                    IExportJPEG exportJPEG = export as IExportJPEG;
                    exportJPEG.Quality = 70;
                    outputResolution   = 70.0;
                }
                break;

            case "PNG":
                export = new ExportPNGClass();
                break;

            case "TIF":
                export = new ExportTIFFClass();
                break;

            case "GIF":
                export = new ExportGIFClass();
                break;

            case "EMF":
                export = new ExportEMFClass();
                break;

            case "SVG":
                export = new ExportSVGClass();
                break;

            case "AI":
                export = new ExportAIClass();
                break;

            case "EPS":
                export = new ExportPSClass();
                break;
            }

            IGraphicsContainer    docGraphicsContainer;
            IElement              docElement;
            IOutputRasterSettings docOutputRasterSettings;
            IMapFrame             docMapFrame;
            IActiveView           tmpActiveView;
            IOutputRasterSettings doOutputRasterSettings;

            if (activeView is IMap)
            {
                doOutputRasterSettings = activeView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
                doOutputRasterSettings.ResampleRatio = 3;
            }
            else if (activeView is IPageLayout)
            {
                doOutputRasterSettings = activeView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
                doOutputRasterSettings.ResampleRatio = 4;
                //and assign ResampleRatio to the maps in the PageLayout.
                docGraphicsContainer = activeView as IGraphicsContainer;
                docGraphicsContainer.Reset();
                docElement = docGraphicsContainer.Next();
                int c = 0;

                while (docElement != null)
                {
                    c += 1;
                    if (docElement is IMapFrame)
                    {
                        docMapFrame             = docElement as IMapFrame;
                        tmpActiveView           = docMapFrame.Map as IActiveView;
                        docOutputRasterSettings = tmpActiveView.ScreenDisplay.DisplayTransformation as IOutputRasterSettings;
                        docOutputRasterSettings.ResampleRatio = 1;
                    }
                    docElement = docGraphicsContainer.Next();
                }


                docMapFrame          = null;
                docGraphicsContainer = null;
                tmpActiveView        = null;
            }
            else
            {
                docOutputRasterSettings = null;
            }
            //end



            export.ExportFileName = filename;
            export.Resolution     = outputResolution;


            tagRECT exportRECT = activeView.ExportFrame;

            exportRECT.right  = (int)(exportRECT.right * (outputResolution / screenResolution));
            exportRECT.bottom = (int)(exportRECT.bottom * (outputResolution / screenResolution));

            IEnvelope envelope = new EnvelopeClass();

            envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
            export.PixelBounds = envelope;

            int hDC = export.StartExporting();

            activeView.Output(hDC, Convert.ToInt32(export.Resolution), ref exportRECT, null, null);
            export.FinishExporting();
            export.Cleanup();
        }
        /// <summary>Exports the page layout to a file.</summary>
        /// <param name="pageLayout">The page layout.</param>
        /// <param name="exportPath">The name of the output file.</param>
        /// <param name="exportFormat">The format of the output.</param>
        /// <param name="dpi">The resolution of the output files.</param>
        void ExportPageLayoutToFile(IPageLayout pageLayout, string exportPath, long dpi, ExportFormat exportFormat)
        {
            IExport export = null;

            // Get the color of the backgrounds for formats that support transparent backgrounds.
            IColor bgColor = null;

            switch (exportFormat)
            {
            case ExportFormat.GIF:
            case ExportFormat.PNG:
                IPage page = pageLayout.Page;
                bgColor = page.BackgroundColor;
                break;
            }


            // Set "export" to the proper type of Export*Class, and set parameters specific to that image type.
            switch (exportFormat)
            {
            case ExportFormat.AI:
                export = new ExportAIClass();
                var ai = export as IExportAI2;
                //ai.EmbedFonts = true;
                break;

            case ExportFormat.EMF:
                export = new ExportEMFClass();
                var xEmf = export as IExportEMF;
                //xEmf.Description =
                break;

            case ExportFormat.PS:
                export = new ExportPSClass();
                var xPs = export as IExportPS;
                xPs.EmbedFonts = true;
                //xPs.Emulsion =
                //xPs.Image =
                //xPs.ImageCompression = esriExportImageCompression.esriExportImageCompressionNone
                //xPs.LanguageLevel = esriExportPSLanguageLevel.esriExportPSLevel3
                break;

            case ExportFormat.SVG:
                export = new ExportSVGClass();
                var xSvg = export as IExportSVG;
                //xSvg.Compressed = true;
                //xSvg.EmbedFonts = true;
                break;

            case ExportFormat.PDF:
                export = new ExportPDFClass();
                var xPdf = export as IExportPDF;
                //xPdf.Compressed = true;
                //xPdf.EmbedFonts = true;
                //xPdf.ImageCompression = esriExportImageCompression.esriExportImageCompressionNone
                break;

            case ExportFormat.GIF:
                export = new ExportGIFClass();
                IExportGIF xGif = export as IExportGIF;
                xGif.TransparentColor = bgColor;
                //xGif.BiLevelThreshold =
                //xGif.CompressionType = esriGIFCompression.esriGIFCompressionNone;
                //xGif.InterlaceMode = false;
                break;

            case ExportFormat.PNG:
                export = new ExportPNGClass();
                IExportPNG xPng = export as IExportPNG;
                //xPng.BiLevelThreshold =
                //xPng.InterlaceMode = false;
                xPng.TransparentColor = bgColor;
                break;

            case ExportFormat.BMP:
                export = new ExportBMPClass();
                IExportBMP xBmp = export as IExportBMP;
                //xBmp.BiLevelThreshold =
                //xBmp.RLECompression = true;
                break;

            case ExportFormat.JPEG:
                export = new ExportJPEGClass();
                IExportJPEG xJpg = export as IExportJPEG;
                //xJpg.ProgressiveMode = false;
                //xJpg.Quality = 80;  //The JPEG compression / image quality. Range (0..100). Default is 100 (no compression / best quality).
                break;

            case ExportFormat.TIFF:
                export = new ExportTIFFClass();
                IExportTIFF xTif = export as IExportTIFF;
                //xTif.BiLevelThreshold =
                //xTif.CompressionType =
                //xTif.GeoTiff = true;
                //The JPEG or Deflate (depending on the Compression type) compression / image quality. Range (0..100). Default is 100 (no compression / best quality).
                //xTif.JPEGOrDeflateQuality = 100;
                break;

            default:
                const string MSG_FMT = "Support for \"{0}\" export is not yet implemented.";
                string       message = string.Format(MSG_FMT, Enum.GetName(typeof(ExportFormat), exportFormat));
                throw new NotImplementedException(message);
            }

            IEnvelope pixelEnv   = new EnvelopeClass();
            IEnvelope pageExt    = GetPageExtent(pageLayout);
            IPoint    upperRight = pageExt.UpperRight;

            pixelEnv.PutCoords(0, 0, dpi * upperRight.X, dpi * upperRight.Y);
            export.PixelBounds    = pixelEnv;
            export.Resolution     = dpi;
            export.ExportFileName = exportPath;
            //
            //(device coordinates origin is upper left, ypositive is down)
            tagRECT expRect;

            expRect.left   = (int)export.PixelBounds.LowerLeft.X;
            expRect.bottom = (int)export.PixelBounds.UpperRight.Y;
            expRect.right  = (int)export.PixelBounds.UpperRight.X;
            expRect.top    = (int)export.PixelBounds.LowerLeft.Y;

            _app.StatusBar.set_Message(0, string.Format("exporting \"{0}\"", exportPath));
            long        hdc = export.StartExporting();
            IActiveView av  = (IActiveView)pageLayout;

            av.Output((int)hdc, (int)dpi, ref expRect, null, null);
            export.FinishExporting();
            export.Cleanup();
        }
        /// <summary>
        /// Create Bitmap From ESRI Dataset
        /// </summary>
        /// <param name="dataset">Dataset to generate an image from</param>
        /// <param name="imageFormat">Output image format</param>
        /// <param name="size">Size of output image</param>
        /// <param name="resolution">Resolution of output image (dpi)</param>
        /// <param name="background">Background color</param>
        /// <param name="filename">Ouput filename</param>
        public static void CreateBitmap(IDataset dataset, esriImageFormat imageFormat, Size size, ushort resolution, Color background, string filename)
        {
            ILayer layer = null;

            switch (dataset.Type)
            {
            case esriDatasetType.esriDTFeatureClass:
                IFeatureClass featureClass = (IFeatureClass)dataset;
                switch (featureClass.FeatureType)
                {
                case esriFeatureType.esriFTDimension:
                    layer = new DimensionLayerClass();
                    break;

                case esriFeatureType.esriFTAnnotation:
                    layer = new FeatureLayerClass();
                    IGeoFeatureLayer geoFeaureLayer = (IGeoFeatureLayer)layer;
                    geoFeaureLayer.DisplayAnnotation = true;
                    break;

                case esriFeatureType.esriFTComplexEdge:
                case esriFeatureType.esriFTComplexJunction:
                case esriFeatureType.esriFTSimple:
                case esriFeatureType.esriFTSimpleEdge:
                case esriFeatureType.esriFTSimpleJunction:
                    layer = new FeatureLayerClass();
                    break;
                }
                if (layer == null)
                {
                    return;
                }

                IFeatureLayer featureLayer = (IFeatureLayer)layer;
                featureLayer.FeatureClass = featureClass;

                break;

            case esriDatasetType.esriDTRasterDataset:
                layer = new RasterLayerClass();
                IRasterLayer rasterLayer = (IRasterLayer)layer;
                rasterLayer.CreateFromDataset((IRasterDataset)dataset);
                break;

            default:
                string message = string.Format("[{0}] is not supported", dataset.Type.ToString());
                throw new Exception(message);
            }
            if (layer == null)
            {
                return;
            }

            // Create In-memory Map
            IMap map = new MapClass();

            map.AddLayer(layer);
            IActiveView activeView = (IActiveView)map;
            IExport     export     = null;
            tagRECT     rect       = new tagRECT();

            // Set Format Specific Properties
            switch (imageFormat)
            {
            case esriImageFormat.esriImageJPG:
                export = new ExportJPEGClass();
                IExportJPEG exportJpeg = (IExportJPEG)export;
                exportJpeg.ProgressiveMode = false;
                exportJpeg.Quality         = 100;

                break;

            default:
                throw new Exception("[" + imageFormat.ToString() + "] is not supported");
            }
            if (export == null)
            {
                throw new Exception("Failed to Created Exporter");
            }

            // Set Background
            if ((export is IExportBMP) ||
                (export is IExportGIF) ||
                (export is IExportJPEG) ||
                (export is IExportPNG) ||
                (export is IExportTIFF))
            {
                IExportImage exportImage = (IExportImage)export;
                exportImage.ImageType       = esriExportImageType.esriExportImageTypeTrueColor;
                exportImage.BackgroundColor = GeodatabaseUtility.ToESRIColor(background);
            }

            // Set Export Frame
            rect        = activeView.ExportFrame;
            rect.left   = 0;
            rect.top    = 0;
            rect.right  = size.Width;
            rect.bottom = size.Height;

            // Set Output Extent
            IEnvelope envelope = new EnvelopeClass();

            envelope.PutCoords(rect.left, rect.top, rect.right, rect.bottom);
            export.PixelBounds    = envelope;
            export.Resolution     = resolution;
            export.ExportFileName = filename;

            // Export map to image
            int intHdc = export.StartExporting();

            activeView.Output(intHdc, resolution, ref rect, null, null);
            export.FinishExporting();
            export.Cleanup();

            // Clear Layers
            map.ClearLayers();

            // Release COM Objects
            GeodatabaseUtility.ReleaseComObject(layer);
            GeodatabaseUtility.ReleaseComObject(envelope);
            GeodatabaseUtility.ReleaseComObject(map);
            GeodatabaseUtility.ReleaseComObject(activeView);
            GeodatabaseUtility.ReleaseComObject(export);
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }