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


Export MOLE Cached Graphic To File Snippet (ArcObjects .NET 10.6 SDK)
ArcObjects Library Reference

Export MOLE Cached Graphic To File Snippet

Exports a Cached Graphic to an image file.

[C#]
///<summary>Exports a Cached Graphic to an image file.</summary>
///  
///<param name="cachedGraphic">An ICachedGraphic interface.</param>
///<param name="display">An IDisplay interface. (ActiveView.ScreenDisplay if possible)</param>
///<param name="exportPathAndFile">A System.String that is the fully qualified filename to export to. Example: "C:\temp\bob.jpg"</param>
///<param name="exportImageSize">A System.Int32 that is the pixel heigth/width size of the exported image file, between 5 and 2000 - limited to practical sizes or it would take unrealistically long. Example: 1024</param>
///   
///<returns>A System.Boolean where true if successful, false otherwise.</returns>
///   
///<remarks></remarks>
public System.Boolean ExportMOLECachedGraphicToFile(ESRI.ArcGIS.DefenseSolutions.ICachedGraphic cachedGraphic, ESRI.ArcGIS.Display.IDisplay display, System.String exportPathAndFile, System.Int32 exportImageSize)
{
  System.Boolean success=false;

  try
  {
    // Is it is a valid graphic
    if (cachedGraphic == null)
    {
      System.Diagnostics.Trace.WriteLine("Null Graphic passed to ExportCachedGraphicToFile");
      return success; // i.e. fail
    }

    System.IO.FileInfo fileInfo=new System.IO.FileInfo(exportPathAndFile);

    System.String lowerImageExtension=fileInfo.Extension.ToLower();

    // check for valid extensions
    if ((lowerImageExtension != ".png") && (lowerImageExtension != ".bmp") &&
        (lowerImageExtension != ".gif") && (lowerImageExtension != ".tif") &&
        (lowerImageExtension != ".jpg") && (lowerImageExtension != ".jpeg"))
    {
       System.Diagnostics.Trace.WriteLine("Could not perform export, invalid image extension selected: " + lowerImageExtension);
       return success; // i.e. fail
    }

    // check for valid export size
    if ((exportImageSize < 5) || (exportImageSize > 2000))
    {
      System.Diagnostics.Trace.WriteLine("Could not perform export, invalid image size selected: " + exportImageSize + " must be between 5..2000");

      return success; // i.e. fail
    }

    ESRI.ArcGIS.DefenseSolutions.IExportGraphic exportGraphic=cachedGraphic as ESRI.ArcGIS.DefenseSolutions.IExportGraphic;

    if (exportGraphic == null)
    {
      System.Diagnostics.Trace.WriteLine("Could not convert graphic to ExportGraphic. Aborting");
      return success; // i.e. fail
    }

    success=exportGraphic.ExportToFile(display, exportPathAndFile, null, exportImageSize, exportImageSize, exportImageSize, 1.2, 100.0, false);

  }
  catch (System.Exception ex)
  {
    System.Diagnostics.Trace.WriteLine(ex.StackTrace);
    System.Diagnostics.Trace.WriteLine(ex.Message);
  }

  return success;
}
[Visual Basic .NET]
'''<summary>Exports a Cached Graphic to an image file.</summary>
'''  
'''<param name="cachedGraphic">An ICachedGraphic interface.</param>
'''<param name="display">An IDisplay interface. (ActiveView.ScreenDisplay if possible)</param>
'''<param name="exportPathAndFile">A System.String that is the fully qualified filename to export to. Example: "C:\temp\bob.jpg"</param>
'''<param name="exportImageSize">A System.Int32 that is the pixel heigth/width size of the exported image file, between 5 and 2000 - limited to practical sizes or it would take unrealistically long. Example: 1024</param>
'''   
'''<returns>A System.Boolean where true if successful, false otherwise.</returns>
'''   
'''<remarks></remarks>
Public Function ExportMOLECachedGraphicToFile(ByVal cachedGraphic As ESRI.ArcGIS.DefenseSolutions.ICachedGraphic, ByVal display As ESRI.ArcGIS.Display.IDisplay, ByVal exportPathAndFile As System.String, ByVal exportImageSize As System.Int32) As System.Boolean

  Dim success As System.Boolean=False

  Try

    ' Is it is a valid graphic
    If cachedGraphic Is Nothing Then
      System.Diagnostics.Trace.WriteLine("Nothing Graphic passed to ExportCachedGraphicToFile")
      Return success
    End If

    Dim fileInfo As System.IO.FileInfo=New System.IO.FileInfo(exportPathAndFile)
    Dim lowerImageExtension As System.String=fileInfo.Extension.ToLower

    ' check for valid extensions
    If (Not (lowerImageExtension=".png")) AndAlso (Not (lowerImageExtension=".bmp")) AndAlso (Not (lowerImageExtension=".gif")) AndAlso (Not (lowerImageExtension=".tif")) AndAlso (Not (lowerImageExtension=".jpg")) AndAlso (Not (lowerImageExtension=".jpeg")) Then
      System.Diagnostics.Trace.WriteLine("Could not perform export, invalid image extension selected: " + lowerImageExtension)
      Return success
    End If

    ' check for valid export size
    If (exportImageSize < 5) OrElse (exportImageSize > 2000) Then
      System.Diagnostics.Trace.WriteLine("Could not perform export, invalid image size selected: " + exportImageSize.ToString + " must be between 5..2000")
      Return success
    End If

    Dim exportGraphic As ESRI.ArcGIS.DefenseSolutions.IExportGraphic=CType(cachedGraphic, ESRI.ArcGIS.DefenseSolutions.IExportGraphic) ' Explicit Cast

    If exportGraphic Is Nothing Then
      System.Diagnostics.Trace.WriteLine("Could not convert graphic to ExportGraphic. Aborting")
      Return success
    End If

    success=exportGraphic.ExportToFile(display, exportPathAndFile, Nothing, exportImageSize, exportImageSize, exportImageSize, 1.2, 100, False)

  Catch ex As System.Exception

    System.Diagnostics.Trace.WriteLine(ex.StackTrace)
    System.Diagnostics.Trace.WriteLine(ex.Message)

  End Try

  Return success

End Function

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