This document is archived and information here might be outdated.  Recommended version.


Create JPEG from ActiveView Snippet (ArcObjects .NET 10.4 SDK)
ArcObjects Library Reference

Create JPEG from ActiveView Snippet

Creates a .jpg (JPEG) file from IActiveView. Default values of 96 DPI are used for the image creation.

[C#]
///<summary>Creates a .jpg (JPEG) file from IActiveView. Default values of 96 DPI are used for the image creation.</summary>
///
///<param name="activeView">An IActiveView interface</param>
///<param name="pathFileName">A System.String that the path and filename of the JPEG you want to create. Example: "C:\temp\test.jpg"</param>
/// 
///<returns>A System.Boolean indicating the success</returns>
/// 
///<remarks></remarks>
public System.Boolean CreateJPEGFromActiveView(ESRI.ArcGIS.Carto.IActiveView activeView, System.String pathFileName)
{
  //parameter check
  if (activeView == null || !(pathFileName.EndsWith(".jpg")))
  {
    return false;
  }
  ESRI.ArcGIS.Output.IExport export=new ESRI.ArcGIS.Output.ExportJPEGClass();
  export.ExportFileName=pathFileName;

  // Microsoft Windows default DPI resolution
  export.Resolution=96;
  ESRI.ArcGIS.Display.tagRECT exportRECT=activeView.ExportFrame;
  ESRI.ArcGIS.Geometry.IEnvelope envelope=new ESRI.ArcGIS.Geometry.EnvelopeClass();
  envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
  export.PixelBounds=envelope;
  System.Int32 hDC=export.StartExporting();
  activeView.Output(hDC, (System.Int16)export.Resolution, ref exportRECT, null, null);

  // Finish writing the export file and cleanup any intermediate files
  export.FinishExporting();
  export.Cleanup();

  return true;
}
[Visual Basic .NET]
'''<summary>Creates a .jpg (JPEG) file from IActiveView. Default values of 96 DPI are used for the image creation.</summary>
'''
'''<param name="activeView">An IActiveView interface</param>
'''<param name="pathFileName">A System.String that the path and filename of the JPEG you want to create. Example: "C:\temp\test.jpg"</param>
''' 
'''<returns>A System.Boolean indicating the success</returns>
''' 
'''<remarks></remarks>
Public Function CreateJPEGFromActiveView(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal pathFileName As System.String) As System.Boolean

  'parameter check
  If activeView Is Nothing OrElse Not (pathFileName.EndsWith(".jpg")) Then
    Return False
  End If

  Dim export As ESRI.ArcGIS.Output.IExport=New ESRI.ArcGIS.Output.ExportJPEGClass
  export.ExportFileName=pathFileName

  ' Microsoft Windows default DPI resolution
  export.Resolution=96
  Dim exportRECT As ESRI.ArcGIS.Display.tagRECT=activeView.ExportFrame
  Dim envelope As ESRI.ArcGIS.Geometry.IEnvelope=New ESRI.ArcGIS.Geometry.EnvelopeClass
  envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom)
  export.PixelBounds=envelope
  Dim hDC As System.Int32=export.StartExporting
  activeView.Output(hDC, CShort(export.Resolution), exportRECT, Nothing, Nothing)

  ' Finish writing the export file and cleanup any intermediate files
  export.FinishExporting()
  export.Cleanup()

  Return True

End Function

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Carto
  • ESRI.ArcGIS.Display
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.Output
  • ESRI.ArcGIS.System