I have created a console application to export the active view of an MXD document using C# ArcObjects. The app works, but for some reason the exported image is missing all the layers in the map and legend. I checked, all layers are enabled, the problem is in the export implementation.
using ESRI.ArcGIS.esriSystem; using System; using System.Collections.Generic; using System.Text; using ESRI.ArcGIS.Geoprocessor; using ESRI.ArcGIS.Geoprocessing; using ESRI.ArcGIS.DataManagementTools; using ESRI.ArcGIS.Output; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.ADF.Resources; using ESRI.ArcGIS.ArcMapUI; using ESRI.ArcGIS.Framework; using ESRI.ArcGIS.ArcMap; using ESRI.ArcGIS.Geometry; namespace firstConsole { class Program { private static LicenseInitializer m_AOLicenseInitializer = new firstConsole.LicenseInitializer(); [STAThread()] static void Main(string[] args) { ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop); //ESRI License Initializer generated code. m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeBasic }, new esriLicenseExtensionCode[] { }); //ESRI License Initializer generated code. //Do not make any call to ArcObjects after ShutDownApplication() m_AOLicenseInitializer.ShutdownApplication(); IPrintAndExport export = new PrintAndExportClass(); string mxd = @"C:\0_Work\Test\3.mxd"; tagRECT exporterRectangle; exporterRectangle.left = 0; exporterRectangle.bottom = 1654; exporterRectangle.top = 0; exporterRectangle.right = 2340; var expOptions = new ExportJPEGClass(); expOptions.Quality = 75; expOptions.Resolution = 200; IMapDocument pMapDocument = new MapDocumentClass(); expOptions.ExportFileName = mxd.Replace("mxd", "jpg"); pMapDocument.Open(mxd, null); IActiveView activeView = pMapDocument.ActiveView; export.Export(activeView, expOptions, 200, false, null); pMapDocument.Close(); } } }
Here is how I did it:
Brent Hoskisson
IActiveView av = (IActiveView)map;
IExport pExport = new ExportBMPClass
{
ExportFileName = bmpFile
};
tagRECT eRect = av.ExportFrame;
IEnvelope pPBE = new EnvelopeClass();
pPBE.PutCoords(eRect.left, eRect.top, eRect.right, eRect.bottom);
pExport.PixelBounds = pPBE;
try
{
int hDC = pExport.StartExporting();
av.Output(hDC, Convert.ToInt32(pExport.Resolution), ref eRect, null, null);
pExport.FinishExporting();
}
finally
{
pExport.Cleanup();
}