/// <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(); }