Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Given a series of map layers is it possible to arrange them in some order, add basic map features (north arrow, scale bar) and then save the result as a JPG. I'd like to accomplish this in the background as a button-triggered event in an ArcMap Add-On.

I've seen ExportActiveView, but I think this will only work if I have the map layers already loaded into the ActiveView and on the users' screen.

Is it possible to create this map behind the scenes?

share|improve this question
1  
I'm guessing your question at gis.stackexchange.com/questions/15987/… provides a little more background in a different context (for instance that your data lives in a File GeoDatabase, etc)? It doesn't bother me, I am just curious if this is correct. –  elrobis May 4 '12 at 15:08
    
@Radar, FYI this all may be done using python/arcpy too. Adding layers and exporting map. It will take more less code to develop. You could just reference a template mxd that has your layout controls already positioned and created. –  artwork21 May 4 '12 at 15:42
    
@elrobis You can consider that question irrelevant for the purpose of this one - I just have map layers coming in and I need a JPG out, all done in the background (e.g. not on screen). –  Nick Ochoski May 4 '12 at 18:49
add comment

1 Answer 1

up vote 6 down vote accepted

Something similar I have done in the past is use IMapDocument. It does require a hard file, so I just create a temp file for the process, and delete after the export. In the code snippet below, the MapExportFormat is a little enumerator I created representing different output formats (pdf,gif,png).

private static void ExportMap(IMapDocument map,MapExportFormat exportFormat, string outputFile)
        {
            int OUTPUT_RES = 600;
            IExport export = null;

            switch (exportFormat)
            {
                case MapExportFormat.PDF:
                    IExportPDF pdfExport = new ExportPDFClass();
                    pdfExport.ImageCompression = esriExportImageCompression.esriExportImageCompressionDeflate;
                    pdfExport.EmbedFonts = true;
                    pdfExport.Compressed = true;
                    //IExportPDF2 pdfExport2 = (IExportPDF2)pdfExport;
                    //pdfExport2.ExportPDFLayersAndFeatureAttributes = esriExportPDFLayerOptions.esriExportPDFLayerOptionsLayersOnly;
                    export = (IExport)pdfExport;
                    break;
                case MapExportFormat.GIF:
                    IExportGIF gifExport = new ExportGIFClass();
                    export = (IExport)gifExport;
                    break;
                case MapExportFormat.JPG:
                    IExportJPEG jpgExport = new ExportJPEGClass();
                    export = (IExport)jpgExport;
                    break;
                case MapExportFormat.PNG:
                    IExportPNG pngExport = new ExportPNGClass();
                    export = (IExport)pngExport;
                    break;

            }

            export.Resolution = OUTPUT_RES;
            export.ExportFileName = outputFile;

            tagRECT exportFrame = map.ActiveView.ExportFrame;
            IEnvelope exportEnvelope = new EnvelopeClass();
            exportEnvelope.PutCoords(exportFrame.left, exportFrame.top, exportFrame.right, exportFrame.bottom);
            export.PixelBounds = exportEnvelope;

            int hdc = export.StartExporting();
            map.ActiveView.Output(hdc, OUTPUT_RES,exportFrame,null,null);
            export.FinishExporting();
            export.Cleanup();
        }
share|improve this answer
    
Thanks for the reply and code snippet. In your example does all of the work happen off-screen? Can ActiveView be used without actually showing it to the user in ArcMap? –  Nick Ochoski May 4 '12 at 18:51
1  
Sure does. I did everything from a button click in a form totally outside of ArcMap. –  user890 May 4 '12 at 19:19
1  
Thanks a lot for the help. One additional point. When working from within an ESRI Add-On and exporting a map document that is not currently on screen, it's best to set the activeView to mapdoc.PageLayout - if you don't set it to the layout view the map unit-to-pixel conversion gets mangled and you end up with image dimensions that are very large and can't be handled by the export.FinishExporting method - it error checks this. –  Nick Ochoski May 7 '12 at 23:23
1  
How do you create the IMapDocument? In my example i had to create a dummy mxd. Like: IMapDocument ipMapDoc; ipMapDoc = new MapDocumentClass(); ipMapDoc.Open(@"D:\Data\Spatial\Untitled.mxd"); Is there any way to get around this? –  user8045 Jun 7 '12 at 8:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.