This sample demonstrates how to export the active view to any of the ten supported graphics file formats. Using this sample in its current state will export a JPEG file to C:/. You can easily modify it to produce other export formats, saved to a location of your choice. Products:
ArcView: VBA, VC++
Platforms: Windows
Minimum ArcGIS Release: 9.0
How to use:
[VBA]
Paste this code into the Visual Basic editor and select Run Sub from the Run menu.
[VC++]
Paste the function in your project.
Call the function from your code.
[VBA]
Public Sub ExportActiveView()
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pActiveView As IActiveView
Set pActiveView = pMxDoc.ActiveView
'Create an ExportJPEG object and QI the pExport interface pointer onto it.
' To export to a format other than JPEG, simply create a different CoClass here
' and change the file extension of the ExportFileName.
Dim pExport As IExport
Set pExport = New ExportJPEG
pExport.ExportFileName = "C:/ExportTest1.jpg"
'Set the export object resolution to 96 dpi. The default screen resolution for
' MS Windows is usually 96dpi, and this value is also the default resolution for all
' of the ArcGIS image format exporters. The vector format exporters (PDF, AI, etc.)
' default to 300 dpi, so we should explicitly set resolution here to be certain we
' are working with a resolution of 96.
pExport.Resolution = 96
'For both PageLayout and Map objects, the ExportFrame property always holds a tagRECT
' structure appropriate for exporting. The values in the tagRECT correspond to the width
' and height to export, measured in pixels with an origin in the top left corner.
Dim exportRECT As tagRECT
exportRECT = pActiveView.ExportFrame
'Create a new envelope object and populate it with the values from exportRECT.
' We need to do this because the exporter object requires an envelope object
' instead of a tagRECT structure.
Dim pPixelBoundsEnv As IEnvelope
Set pPixelBoundsEnv = New Envelope
pPixelBoundsEnv.PutCoords exportRECT.Left, exportRECT.Top, exportRECT.Right, exportRECT.bottom
'Assign the envelope object to the exporter object's PixelBounds property. The exporter object
' will use these dimensions when allocating memory for the export file.
pExport.PixelBounds = pPixelBoundsEnv
'Initialize the exporter object and store it's device context in the hDC variable. At this method
' call, the exporter object will create an empty file and allocate memory based on resolution,
' bit depth, and pixel bounds.
Dim hDC As Long
hDC = pExport.StartExporting
'Redraw the active view, rendering it to the exporter object device context instead of the app display.
'We pass the following values:
' * hDC is the device context of the exporter object.
' * pExport.Resolution is 96, the default resolution value for all image export formats. Default screen
' resolution for MS Windows is usually 96dpi.
' * exportRECT is the tagRECT structure that describes the dimensions of the view that will be rendered.
' The values in exportRECT should match those held in the exporter object's PixelBounds property.
pActiveView.Output hDC, pExport.Resolution, exportRECT, Nothing, Nothing
'Finish writing the export file and cleanup any intermediate files.
pExport.FinishExporting
pExport.Cleanup
End Sub
Public Sub ExportActiveView2()
Dim pMxDoc As IMxDocument
Dim pActiveView As IActiveView
Dim pExport As IExport
Dim pPixelBoundsEnv As IEnvelope
Dim exportRECT As tagRECT
Dim iOutputResolution As Integer
Dim iScreenResolution As Integer
Dim hDC As Long
Set pMxDoc = Application.Document
Set pActiveView = pMxDoc.ActiveView
Set pExport = New ExportJPEG
pExport.ExportFileName = "C:/ExportTest2." & Right(pExport.Filter, 3)
'Because we are exporting to a resolution that differs from screen resolution, we should
' assign the two values to variables for use in our sizing calculations
iScreenResolution = 96 'default screen resolution is usually 96dpi
iOutputResolution = 300
pExport.Resolution = iOutputResolution
'The ExportFrame property gives us the dimensions appropriate for an export at screen resolution.
' Because we are exporting at a higher resolution (more pixels), we must multiply each dimesion
' by the ratio of OutputResolution to ScreenResolution. Instead of assigning the entire
' ExportFrame directly to the exportRECT, let's bring the values across one at a time and multiply
' the dimensions.
With exportRECT
.Left = 0
.Top = 0
.Right = pActiveView.ExportFrame.Right * (iOutputResolution / iScreenResolution)
.bottom = pActiveView.ExportFrame.bottom * (iOutputResolution / iScreenResolution)
End With
'Set up the PixelBounds envelope to match the exportRECT
Set pPixelBoundsEnv = New Envelope
pPixelBoundsEnv.PutCoords exportRECT.Left, exportRECT.Top, exportRECT.Right, exportRECT.bottom
pExport.PixelBounds = pPixelBoundsEnv
hDC = pExport.StartExporting
pActiveView.Output hDC, pExport.Resolution, exportRECT, Nothing, Nothing
pExport.FinishExporting
pExport.Cleanup
End Sub
[VC++]
HRESULT ExportActiveView()
{
IDocumentPtr ipDoc;
m_ipApp->get_Document(&ipDoc);
IMxApplicationPtr ipMxApp(m_ipApp);
IMxDocumentPtr ipMxDoc(ipDoc);
IActiveViewPtr ipActiveView;
ipMxDoc->get_ActiveView(&ipActiveView);
// Create an ExportJPEG object and QI the pExport interface pointer onto it.
// To export to a format other than JPEG, simply create a different CoClass here
// and change the file extension of the ExportFileName.
IExportPtr ipExport(CLSID_ExportJPEG);
ipExport->put_ExportFileName(L"C://ExportTest1.jpg");
// Set the export object resolution to 96 dpi. The default screen resolution for
// MS Windows is usually 96dpi, and this value is also the default resolution for all
// of the ArcGIS image format exporters. The vector format exporters (PDF, AI, etc.)
// default to 300 dpi, so we should explicitly set resolution here to be certain we
// are working with a resolution of 96.
ipExport->put_Resolution(96);
// For both PageLayout and Map objects, the ExportFrame property always holds a tagRECT
// structure appropriate for exporting. The values in the tagRECT correspond to the width
// and height to export, measured in pixels with an origin in the top left corner.
tagRECT exportRECT;
ipActiveView->get_ExportFrame(&exportRECT);
// Create a new envelope object and populate it with the values from exportRECT.
// We need to do this because the exporter object requires an envelope object
// instead of a tagRECT structure.
IEnvelopePtr ipPixelBoundsEnv(CLSID_Envelope);
ipPixelBoundsEnv->PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
// Assign the envelope object to the exporter object's PixelBounds property. The exporter object
// will use these dimensions when allocating memory for the export file.
ipExport->put_PixelBounds(ipPixelBoundsEnv);
// Initialize the exporter object and store it's device context in the hDC variable. At this method
// call, the exporter object will create an empty file and allocate memory based on resolution,
// bit depth, and pixel bounds.
OLE_HANDLE hDC;
ipExport->StartExporting(&hDC);
// Redraw the active view, rendering it to the exporter object device context instead of the app display.
// We pass the following values:
// * hDC is the device context of the exporter object.
// * pExport.Resolution is 96, the default resolution value for all image export formats. Default screen
// resolution for MS Windows is usually 96dpi.
// * exportRECT is the tagRECT structure that describes the dimensions of the view that will be rendered.
// The values in exportRECT should match those held in the exporter object's PixelBounds property.
double dResolution;
ipExport->get_Resolution(&dResolution);
ipActiveView->Output(hDC, (long)dResolution, &exportRECT, 0, 0);
// Finish writing the export file and cleanup any intermediate files.
ipExport->FinishExporting();
ipExport->Cleanup();
return S_OK;
}
HRESULT ExportActiveView2()
{
IDocumentPtr ipDoc;
m_ipApp->get_Document(&ipDoc);
IMxApplicationPtr ipMxApp(m_ipApp);
IMxDocumentPtr ipMxDoc(ipDoc);
IActiveViewPtr ipActiveView;
ipMxDoc->get_ActiveView(&ipActiveView);
IExportPtr ipExport(CLSID_ExportJPEG);
CComBSTR strFilter;
ipExport->get_Filter(&strFilter);
UINT uFilterLength(strFilter.Length() - 3);
CComBSTR strExtension(L"c://ExportTest2.");
strExtension += _tcsninc(strFilter, uFilterLength);
ipExport->put_ExportFileName(strExtension);
// Because we are exporting to a resolution that differs from screen resolution, we should
// assign the two values to variables for use in our sizing calculations
int iScreenResolution = 96; // default screen resolution is usually 96dpi
int iOutputResolution = 300;
ipExport->put_Resolution(iOutputResolution);
// The ExportFrame property gives us the dimensions appropriate for an export at screen resolution.
// Because we are exporting at a higher resolution (more pixels), we must multiply each dimesion
// by the ratio of OutputResolution to ScreenResolution. Instead of assigning the entire
// ExportFrame directly to the exportRECT, let's bring the values across one at a time and multiply
// the dimensions.
tagRECT exportFrame;
ipActiveView->get_ExportFrame(&exportFrame);
tagRECT exportRECT;
exportRECT.left = 0;
exportRECT.top = 0;
exportRECT.right = exportFrame.right * (iOutputResolution / iScreenResolution);
exportRECT.bottom = exportFrame.bottom * (iOutputResolution / iScreenResolution);
// Set up the PixelBounds envelope to match the exportRECT
IEnvelopePtr ipPixelBoundsEnv(CLSID_Envelope);
ipPixelBoundsEnv->PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
ipExport->put_PixelBounds(ipPixelBoundsEnv);
OLE_HANDLE hDC;
ipExport->StartExporting(&hDC);
double dResolution;
ipExport->get_Resolution(&dResolution);
ipActiveView->Output(hDC, (long)dResolution, &exportRECT, 0, 0);
ipExport->FinishExporting();
ipExport->Cleanup();
return S_OK;
}