Export Activeview as JPEG

528
3
08-13-2010 07:31 AM
ChristineHarvey
New Contributor
I've been trying to export the active view of a map to a jpeg, and have only managed to output blank JPEGs.  I'm struggling to see the problem with my code and am posting it here, in the hopes that someone else can see what my problem is.  Thank you in advance.

  static void ExportJPEG(IMxDocument mxDoc)
    {

       

        Int64 lScrRes = (Int64)mxDoc.ActiveView.ScreenDisplay.DisplayTransformation.Resolution;
        IExport pExporter = new ExportJPEGClass();
    

       // IExporter pExporter = new JpegExporterClass() ;
        pExporter.ExportFileName = @"C:\Workspace\Maps\CodeOut\test3.jpeg";
        pExporter.Resolution = (Int32)lScrRes;

        tagRECT deviceRect = mxDoc.ActiveView.ExportFrame; // ScreenDisplay.DisplayTransformation.get_DeviceFrame();
       // tagRECT deviceRect = mxDoc.ActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame();
        IEnvelope pDriverBounds = new EnvelopeClass();
        pDriverBounds.PutCoords(deviceRect.left, deviceRect.top, deviceRect.right, deviceRect.bottom);
      
        pExporter.PixelBounds = pDriverBounds;
      // ITrackCancel pCancel = new CancelTrackerClass();

     //   mxDoc.ActiveView.Output(pExporter.StartExporting(), (Int32)lScrRes, ref deviceRect, mxDoc.ActiveView.Extent, pCancel);
       mxDoc.ActiveView.Output(Convert.ToInt32(pExporter.StartExporting()), Convert.ToInt32 (lScrRes), ref deviceRect, null, null);
        pExporter.FinishExporting();
        pExporter.Cleanup();
   
    }
0 Kudos
3 Replies
NeilClemmons
Regular Contributor III
In the ESRI .NET SDK there is a sample called ExportActiveView.  Have you tried running the C# version of this sample?
0 Kudos
ChristineHarvey
New Contributor
Hi Neil,

Yes actually I ran it this morning.  Just running it as a command in a toolbar, it works perfect.  However, I tried to incorporate the source code into my code by passing as an external dll.  I'm trying to figure out what should be passed at the 'on create'.  I am including the code I've created this morning using this sample.

  static void AddLayerFileToMap(string lyrPath, string shapefilename)
    {
        string document = @"C:\Workspace\Test.mxd";
        //string document = @"\\a3924812\ShapeFileServicesFileDirectory\GeoSpatialData\ESRI\usa\USA Base Map.mxd";

        IDocument pDoc = new MxDocument();

        IApplication pApp = pDoc.Parent;
        pApp.OpenDocument(document);
        ILayer pLayer = MakeLayer((IObjectFactory)pApp, lyrPath, shapefilename);
        IMxDocument pMxDoc = (IMxDocument)pDoc;

        pMxDoc.FocusMap.AddLayer(pLayer);
    

        //ExportJPEG(pMxDoc);
        pApp.SaveDocument(document);
        ExportActiveViewCS_Net.ExportActiveViewCS_Net exportJPEG = new ExportActiveViewCS_Net.ExportActiveViewCS_Net();
        exportJPEG.OnCreate(pApp);
        exportJPEG.OnClick();
        pApp.Shutdown();


    }


PS - I'm a huge coding fan of yours!!
0 Kudos
ChristineHarvey
New Contributor
I have managed to export my active vew as a jpeg.  It seems to have been a resolution issue.  When I run this code in a stand alone application, it works just as expected.  However I have a larger project, where I'm opening an existing map, adding a layer then calling the jpeg export function.  It's strange but, the jpeg export shows the map prior to the layer being added, even though when I open arcmap manually, the added layer is present on the map.  I've been banging my head on this all day.




  I'm posting the jpeg export code here should someone find it useful:




IMapDocument mxDoc = new MapDocumentClass();
                mxDoc.Open(@"C:\Workspace\test.mxd", "");
                IActiveView ActiveView = mxDoc.ActiveView;

                System.Int32 screenResolution = 96;
                System.Int32 outputResolution = 300;

                IExport export = new ExportJPEGClass();
                export.Resolution = outputResolution;
                export.ExportFileName = @"C:\Workspace\Maps\CodeOut\testout2.jpg";

                tagRECT exportRect;
                exportRect.left = 0;
                exportRect.top = 0;
                exportRect.right = 800 *(outputResolution / screenResolution);
                exportRect.bottom = 800 * (outputResolution / screenResolution);

                IEnvelope envelope = new EnvelopeClass();
                envelope.PutCoords(exportRect.left, exportRect.top, exportRect.right, exportRect.bottom);
                export.PixelBounds = envelope;

                Int32 hDC = export.StartExporting();
                ActiveView.Output(hDC, (System.Int16)export.Resolution, ref exportRect, null, null);
                export.FinishExporting();
                export.Cleanup();
0 Kudos