How to display the raster cell value in the status bar


This sample displays the pixel value of the first raster layer in the map. This sample will display multiplane data in the form "(value 1, value 2, value 3)" for three planes.
The code samples in this section show the fundamentals of programming with ArcObjects. A careful reading of them gives you all the important concepts you need for developing with ArcObjects, as well as an introduction to the most important ArcObjects components.
The code can be typed or copied into the VBA environment in ArcMap or ArcCatalog, after which you can follow through with the VBA debugger.

How to use

  1. Add the code to the MouseMove event of a UIToolControl in ArcMap.
[VBA]
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim pActiveView As IActiveView
Set pActiveView = pMxDoc.FocusMap
Dim pPoint As IPoint
Set pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)

Dim pBlockSize As IPnt
Set pBlockSize = New DblPnt
pBlockSize.SetCoords 1#, 1#

Dim pLayer As IRasterLayer
Dim pPixelBlock As IPixelBlock
Dim vValue As Variant
Dim i As Long, j As Long
Dim sPixelVals As String
sPixelVals = "No Raster"
Dim pRasterProps As IRasterProps
Dim dXSize As Double, dYSize As Double
Dim pPixel As IPnt
Set pPixel = New DblPnt
For i = 0 To pMxDoc.FocusMap.LayerCount - 1
    If (TypeOf pMxDoc.FocusMap.Layer(i) Is IRasterLayer) Then
        Set pLayer = pMxDoc.FocusMap.Layer(i)
        Set pPixelBlock = pLayer.Raster.CreatePixelBlock(pBlockSize)
        
        Set pRasterProps = pLayer.Raster
        dXSize = pRasterProps.Extent.XMax - pRasterProps.Extent.XMin
        dYSize = pRasterProps.Extent.YMax - pRasterProps.Extent.YMin
        
        dXSize = dXSize / pRasterProps.Width
        dYSize = dYSize / pRasterProps.Height
        
        pPixel.x = (pPoint.x - pRasterProps.Extent.XMin) / dXSize
        pPixel.y = (pRasterProps.Extent.YMax - pPoint.y) / dYSize
        
        pLayer.Raster.Read pPixel, pPixelBlock
        For j = 0 To pPixelBlock.Planes - 1
            If (sPixelVals = "No Raster") Then
                sPixelVals = "("
            Else
                sPixelVals = sPixelVals & ", "
            End If
            vValue = pPixelBlock.GetVal(j, 0, 0)
            sPixelVals = sPixelVals & CStr(vValue)
        Next j
        If (sPixelVals <> "No Raster") Then sPixelVals = sPixelVals & ")"
        ThisDocument.Parent.StatusBar.Message(0) = "Raster value = " & sPixelVals
        Exit For
    End If
Next i