Arcobjects SDK C# - Export MXD to PNG

5378
5
04-18-2016 03:12 AM
NicolasFaget1
New Contributor III

Hi everyone,

I have a problem with Arcobjects SDK C#. I want to export a MXD to PDF, AI and PNG.

The PDF and AI Export works fine but not the PNG Export.

When I export to PNG, I have this error : Exception de HRESULT : 0x80040334

My call :

IMapDocument pMapDoc = new MapDocument();

pMapDoc.Open(folderFileMXD + nameFile + ".mxd");

IActiveView m_ActiveView = pMapDoc.ActiveView;

ExportActiveViewParameterized(nameFile, m_ActiveView, 300, 1, "PNG", folderFilePNG, false);

My function :

   private static void ExportActiveViewParameterized(string fileName, IActiveView docActiveView, int iOutputResolution, int lResampleRatio, string ExportType, string sOutputDir, Boolean bClipToGraphicsExtent)

        {

            try

            {

                IExport docExport;

                IPrintAndExport docPrintExport;

                IOutputRasterSettings RasterSettings;

                // The Export*Class() type initializes a new export class of the desired type.

                if (ExportType == "PDF")

                {

                    docExport = new ExportPDFClass();

                }

                else if (ExportType == "AI")

                {

                     docExport = new ExportAIClass();

                }

                else if (ExportType == "PNG")

                {

                    docExport = new ExportPNGClass();                   

                }

                else

                {

                     ExportType = "EMF";

                    docExport = new ExportEMFClass();

                }

                docPrintExport = new PrintAndExportClass();

                docExport.ExportFileName = sOutputDir + fileName + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0];

                //// check if export is vector or raster

                if (docExport is IOutputRasterSettings)

                {

                    // for vector formats, assign the desired ResampleRatio to control drawing of raster layers at export time  

                    RasterSettings = (IOutputRasterSettings)docExport;

                    RasterSettings.ResampleRatio = (int)lResampleRatio;

                }

                docPrintExport.Export(docActiveView, docExport, 300, bClipToGraphicsExtent, null);            

            }

            catch (Exception e)

            {

                Log.Error("FAILURE");

                Log.Error(e.Message);

                Log.Error(e.StackTrace);

            }

        }

Do you have an idea of the problem ?

Thanks in advance

0 Kudos
5 Replies
DuncanHornby
MVP Notable Contributor

Looking at the API help file the IOutputRasterSettings interface is not implemented by ExportPNG class. May be that is the issue?

0 Kudos
YuanLiu
Occasional Contributor

I tested this code snippet and both PDF and PNG worked well for me. Though typically it is recommended to activate the active view before use (please refer to http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//0012000008m6000000), seems like this is not the issue for you here. I would recommend to test it in ArcMap add-in first. You may also try with other mxd files.

0 Kudos
NicolasFaget1
New Contributor III

Sorry for my late answer. It works.

Thank you very much.

0 Kudos
DuncanHornby
MVP Notable Contributor

What did you do? It would be useful for others to know how you solved it rather than just saying "it works".

0 Kudos
NicolasFaget1
New Contributor III