ArcObjects Library Reference

Export MOLE Cached Graphic To Bitmap Snippet

Exports a MOLE Cached Graphic to an in memory image as a .NET Bitmap.

[C#]

///<summary>Exports a MOLE Cached Graphic to an in memory image as a .NET Bitmap.</summary>
///  
///<param name="cachedGraphic">An ICachedGraphic interface.</param>
///<param name="display">An IDisplay interface. (ActiveView.ScreenDisplay if possible)</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>
///<param name="color">An IColor interface.</param>
///   
///<returns>A System.Drawing.Bitmap or Nothing if failure.</returns>
///   
///<remarks> Call this method to export a MOLE Cached Graphic to an in memory image.</remarks>
public System.Drawing.Bitmap ExportMOLECachedGraphicToBitmap(ESRI.ArcGIS.DefenseSolutions.ICachedGraphic cachedGraphic, ESRI.ArcGIS.Display.IDisplay display, System.Int32 exportImageSize, ESRI.ArcGIS.Display.IColor color)
{
  System.Drawing.Bitmap exportBM = null;

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

    // check for valid export size
    // there is really no limit to this in MOLE, but 
    // a. making it too small will be meaningless since you won't be able to represent with too few pixels
    // b. making it too large will hamper performance
    if ((exportImageSize < 5) || (exportImageSize > 2000))
    {
      System.Diagnostics.Trace.WriteLine("Could not perform export, invalid image size selected: "
      + exportImageSize + " must be between 5..2000");

      return null; // i.e. fail
    }

    ESRI.ArcGIS.DefenseSolutions.ICreateBitmap createBitmap = cachedGraphic as ESRI.ArcGIS.DefenseSolutions.ICreateBitmap;
    if (createBitmap == null)
    {
      System.Diagnostics.Trace.WriteLine("Could not convert graphic to ICreateBitmap");
      return null; // i.e. fail
    }

    System.Int32 hDIB = createBitmap.DrawToDIB(display, exportImageSize, exportImageSize, 1.2, color);
    System.IntPtr iphDIB = new System.IntPtr(hDIB);
    exportBM = System.Drawing.Bitmap.FromHbitmap(iphDIB); // .NET only Class

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

  return exportBM;
}
[Visual Basic .NET]

'''<summary>Exports a MOLE Cached Graphic to an in memory image as a .NET Bitmap.</summary>
'''  
'''<param name="cachedGraphic">An ICachedGraphic interface.</param>
'''<param name="display">An IDisplay interface. (ActiveView.ScreenDisplay if possible)</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>
'''<param name="color">An IColor interface.</param>
'''   
'''<returns>A System.Drawing.Bitmap or Nothing if failure.</returns>
'''   
'''<remarks> Call this method to export a MOLE Cached Graphic to an in memory image.</remarks>
Public Function ExportMOLECachedGraphicToBitmap(ByVal cachedGraphic As ESRI.ArcGIS.DefenseSolutions.ICachedGraphic, ByVal display As ESRI.ArcGIS.Display.IDisplay, ByVal exportImageSize As System.Int32, ByVal color As ESRI.ArcGIS.Display.IColor) As System.Drawing.Bitmap

  Dim exportBM As System.Drawing.Bitmap = Nothing

  Try

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

    ' check for valid export size
    ' there is really no limit to this in MOLE, but 
    ' a. making it too small will be meaningless since you won't be able to represent with too few pixels
    ' b. making it too large will hamper performance
    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 Nothing
    End If

    Dim createBitmap As ESRI.ArcGIS.DefenseSolutions.ICreateBitmap = CType(cachedGraphic, ESRI.ArcGIS.DefenseSolutions.ICreateBitmap) ' Explict Cast

    If createBitmap Is Nothing Then
      System.Diagnostics.Trace.WriteLine("Could not convert graphic to ICreateBitmap")
      Return Nothing
    End If

    Dim hDIB As System.Int32 = createBitmap.DrawToDIB(display, exportImageSize, exportImageSize, 1.2, color)

    Dim iphDIB As System.IntPtr = New IntPtr(hDIB)
    exportBM = System.Drawing.Bitmap.FromHbitmap(iphDIB)

  Catch ex As System.Exception

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

  End Try

  Return exportBM

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.System
  • System
  • System.Drawing