ArcObjects Library Reference

Create JPEG (hi-resolution) from ActiveView Snippet

Creates a .jpg (JPEG) file from IActiveView using a high resolution exporting option. Default values of 96 DPI are overwritten to 300 used for the image creation.

[C#]

///<summary>Creates a .jpg (JPEG) file from the ActiveView using a high resolution exporting option. Default values of 96 DPI are overwritten to 300 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\hiResolutionTest.jpg"</param>
/// 
///<returns>A System.Boolean indicating the success</returns>
/// 
///<remarks></remarks>
public System.Boolean CreateJPEGHiResolutionFromActiveView(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;

  // 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
  System.Int32 screenResolution = 96;
  System.Int32 outputResolution = 300;

  export.Resolution = outputResolution;

  ESRI.ArcGIS.Display.tagRECT exportRECT; // This is a structure
  exportRECT.left = 0;
  exportRECT.top = 0;
  exportRECT.right = activeView.ExportFrame.right * (outputResolution / screenResolution);
  exportRECT.bottom = activeView.ExportFrame.bottom * (outputResolution / screenResolution);

  // Set up the PixelBounds envelope to match the exportRECT
  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); // Explicit Cast and 'ref' keyword needed 
  export.FinishExporting();
  export.Cleanup();

  return true;
}
[Visual Basic .NET]

'''<summary>Creates a .jpg (JPEG) file from the ActiveView using a high resolution exporting option. Default values of 96 DPI are overwritten to 300 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\hiResolutionTest.jpg"</param>
''' 
'''<returns>A System.Boolean indicating the success</returns>
''' 
'''<remarks></remarks>
Public Function CreateJPEGHiResolutionFromActiveView(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

  ' 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
  Dim screenResolution As System.Int32 = 96
  Dim outputResolution As System.Int32 = 300

  export.Resolution = outputResolution

  Dim exportRECT As ESRI.ArcGIS.Display.tagRECT ' This is a structure
  exportRECT.left = 0
  exportRECT.top = 0
  exportRECT.right = CInt(activeView.ExportFrame.right * (outputResolution / screenResolution))
  exportRECT.bottom = CInt(activeView.ExportFrame.bottom * (outputResolution / screenResolution))

  ' Set up the PixelBounds envelope to match the exportRECT
  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) ' Explicit Cast and 'ref' keyword needed
  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