Export Active View to JPEG

Created:8/8/2000
Description:

This sample demonstrates how to export the active view, either the page layout or data view, to a JPEG. There are two samples in this tip. Both samples are similar: the first one creates a JPEG with the default screen resolution, usually 96 dpi; the second one explicitly sets a resolution of 300 dpi.

How to use:
  1. Paste the code into VBA.
  2. Load a map document or create one.
  3. Modify the code to specify the desired JPEG file name and location.
  4. Set ArcMap's active view to either the page layout or the data view.
  5. Execute the ExportActiveView macro.
Option Explicit

Public Sub ExportActiveView()
  Dim pMxDoc As IMxDocument
  Dim pActiveView As IActiveView
  Dim pExporter As IExporter
  Dim pEnv As IEnvelope
  Dim exportFrame As tagRECT
  Dim hdc As Long
  Dim dpi As Integer
  
  Set pMxDoc = Application.Document
  Set pActiveView = pMxDoc.ActiveView
  Set pExporter = New JpegExporter
  Set pEnv = New Envelope
  
  'Setup the exporter
  exportFrame = pActiveView.exportFrame
  pEnv.PutCoords exportFrame.Left, exportFrame.top, exportFrame.Right, exportFrame.bottom
  dpi = pExporter.Resolution 'default screen resolution is usually 96
  
  With pExporter
    .PixelBounds = pEnv
    .ExportFileName = "d:\MyImage.jpg"
    .Resolution = dpi
  End With
  
  'Do the export
  hdc = pExporter.StartExporting
  pActiveView.Output hdc, dpi, exportFrame, Nothing, Nothing
  pExporter.FinishExporting
  
End Sub

Public Sub ExportActiveView2()
  Dim pMxDoc As IMxDocument
  Dim pActiveView As IActiveView
  Dim pExporter As IExporter
  Dim pEnv As IEnvelope
  Dim exportFrame As tagRECT
  Dim dpi As Integer
  Dim xMin As Double
  Dim yMin As Double
  Dim xMax As Double
  Dim yMax As Double
  Dim hdc As Long
  
  Set pMxDoc = Application.Document
  Set pActiveView = pMxDoc.ActiveView
  Set pExporter = New JpegExporter
  Set pEnv = New Envelope
  
  'Setup the exporter
  exportFrame = pActiveView.exportFrame
  pEnv.PutCoords exportFrame.Left, exportFrame.top, exportFrame.Right, exportFrame.bottom
  dpi = 300 'set a higher resolution
  
  With pExporter
    .PixelBounds = pEnv
    .ExportFileName = "d:\MyImage2.jpg"
    .Resolution = dpi
  End With
  
  'Recalc the export frame to handle the increased number of pixels
  Set pEnv = pExporter.PixelBounds
  pEnv.QueryCoords xMin, yMin, xMax, yMax
  exportFrame.Left = xMin
  exportFrame.top = yMin
  exportFrame.Right = xMax
  exportFrame.bottom = yMax

  'Do the export
  hdc = pExporter.StartExporting
  pActiveView.Output hdc, dpi, exportFrame, Nothing, Nothing
  pExporter.FinishExporting
  
End Sub