ArcGIS Pro 3.0 API Reference Guide
ArcGIS.Core.Data.Raster Namespace / Raster Class / Read Method
Top-left corner x offset.
Top-left corner y offset.
The PixelBlock to be read.
Example

In This Topic
    Read Method
    In This Topic
    Read a PixelBlock starting from the top-left corner. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax

    Parameters

    topLeftCornerX
    Top-left corner x offset.
    topLeftCornerY
    Top-left corner y offset.
    pixelBlock
    The PixelBlock to be read.
    Example
    Read and Write pixels from and to a raster dataset using pixel blocks
    await QueuedTask.Run(() =>
    {
      // Create a full raster from the raster dataset.
      ArcGIS.Core.Data.Raster.Raster raster = rasterDataset.CreateFullRaster();
    
      // Calculate size of pixel block to create. Use 128 or height/width of the raster, whichever is smaller.
      var height = raster.GetHeight();
      var width = raster.GetWidth();
      int pixelBlockHeight = height > 128 ? 128 : height;
      int pixelBlockWidth = width > 128 ? 128 : width;
    
      // Create a new (blank) pixel block.
      PixelBlock currentPixelBlock = raster.CreatePixelBlock(pixelBlockWidth, pixelBlockHeight);
    
      // Read pixel values from the raster dataset into the pixel block starting from the given top left corner.
      raster.Read(0, 0, currentPixelBlock);
    
      // Do something with the pixel block...
    
      // Write the pixel block to the raster dataset starting from the given top left corner.
      raster.Write(0, 0, currentPixelBlock);
    });
    Process pixels using a pixel block
    await QueuedTask.Run(() =>
    {
      // Read pixel values from the raster dataset into the pixel block starting from the given top left corner.
      raster.Read(0, 0, currentPixelBlock);
    
      // For each plane (band) in the pixel block
      for (int plane = 0; plane < currentPixelBlock.GetPlaneCount(); plane++)
      {
        // Get a copy of the array of pixels from the pixel block corresponding to the current plane.
        Array sourcePixels = currentPixelBlock.GetPixelData(plane, true);
        // Get the height and width of the pixel block.
        int pBHeight = currentPixelBlock.GetHeight();
        int pBWidth = currentPixelBlock.GetWidth();
    
        // Iterate through the pixels in the array.
        for (int i = 0; i < pBHeight; i++)
        {
          for (int j = 0; j < pBWidth; j++)
          {
            // Get the NoData mask value to see if the pixel is a valid pixel.
            if (Convert.ToByte(currentPixelBlock.GetNoDataMaskValue(plane, j, i)) == 1)
            {
              // Get the pixel value from the array and process it (add 5 to the value).
              // Note: This is assuming the pixel type is Unisigned 8bit.
              int pixelValue = Convert.ToInt16(sourcePixels.GetValue(j, i)) + 5;
              // Make sure the pixel value does not go above the range of the pixel type.
              pixelValue = pixelValue > 254 ? 254 : pixelValue;
              // Set the new pixel value to the array.
              // Note: This is assuming the pixel type is Unisigned 8bit.
              sourcePixels.SetValue(Convert.ToByte(pixelValue), j, i);
            }
          }
        }
        // Set the modified array of pixels back to the pixel block.
        currentPixelBlock.SetPixelData(plane, sourcePixels);
      }
      // Write the pixel block to the raster dataset starting from the given top left corner.
      raster.Write(0, 0, currentPixelBlock);
    });
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 8.1

    See Also