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


Convert Display Pixels to Map Units Snippet (ArcObjects .NET 10.6 SDK)
ArcObjects Library Reference

Convert Display Pixels to Map Units Snippet

Convert the display extents in Pixels (at the current map scale) and then return out the map units.

[C#]
///<summary>Convert the display extents in Pixels (at the current map scale) and then return out the map units.</summary>
///
///<param name="activeView">An IActiveView interface</param>
///<param name="pixelUnits">A System.Double containing the number of display pixels to convert. Example: 100</param>
/// 
///<returns>A System.Double containing the number of Map Units, -1 is returned if something went wrong.</returns>
/// 
///<remarks></remarks>
public System.Double ConvertPixelsToMapUnits(ESRI.ArcGIS.Carto.IActiveView activeView, System.Int32 pixelUnits)
{
  
  if(activeView == null)
  {
    return -1;
  }
  //Get the ScreenDisplay
  ESRI.ArcGIS.Display.IScreenDisplay screenDisplay=activeView.ScreenDisplay;

  //Get the DisplayTransformation 
  ESRI.ArcGIS.Display.IDisplayTransformation displayTransformation=screenDisplay.DisplayTransformation;

  //Get the device frame which will give us the number of pixels in the X direction
  ESRI.ArcGIS.Display.tagRECT deviceRECT=displayTransformation.get_DeviceFrame();
  System.Int32 pixelExtent=(deviceRECT.right - deviceRECT.left);

  //Get the map extent of the currently visible area
  ESRI.ArcGIS.Geometry.IEnvelope envelope=displayTransformation.VisibleBounds;
  System.Double realWorldDisplayExtent=envelope.Width;

  //Calculate the size of one pixel
  if(pixelExtent == 0)
  {
    return -1;
  }
  System.Double sizeOfOnePixel=(realWorldDisplayExtent / pixelExtent);

  //Multiply this by the input argument to get the result
  return (pixelUnits * sizeOfOnePixel);
}
[Visual Basic .NET]
'''<summary>Convert the display extents in Pixels (at the current map scale) and then return out the map units.</summary>
'''
'''<param name="activeView">An IActiveView interface</param>
'''<param name="pixelUnits">A System.Double containing the number of display pixels to convert. Example: 100</param>
''' 
'''<returns>A System.Double containing the number of Map Units, -1 is returned if something went wrong.</returns>
''' 
'''<remarks></remarks>
Public Function ConvertPixelsToMapUnits(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal pixelUnits As System.Int32) As System.Double

  If activeView Is Nothing Then
        Return -1
  End If

  'Get the ScreenDisplay
  Dim screenDisplay As ESRI.ArcGIS.Display.IScreenDisplay=activeView.ScreenDisplay

  'Get the DisplayTransformation 
  Dim displayTransformation As ESRI.ArcGIS.Display.IDisplayTransformation=screenDisplay.DisplayTransformation

  'Get the device frame which will give us the number of pixels in the X direction
  Dim deviceRECT As ESRI.ArcGIS.Display.tagRECT=displayTransformation.DeviceFrame
  Dim pixelExtent As System.Int32=(deviceRECT.right - deviceRECT.left)

  'Get the map extent of the currently visible area
  Dim envelope As ESRI.ArcGIS.Geometry.IEnvelope=displayTransformation.VisibleBounds
  Dim realWorldDisplayExtent As System.Double=envelope.Width

  'Calculate the size of one pixel
  If pixelExtent=0 Then
          Return -1
  End If

  Dim sizeOfOnePixel As System.Double=(realWorldDisplayExtent / pixelExtent)

  'Multiply this by the input argument to get the result
  Return (pixelUnits * sizeOfOnePixel)

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