How to convert a raster to bytes


This sample shows how to convert a raster to bytes and open a raster dataset from bytes.

How to use

  1. Reference libraries: ESRI.ArcGIS.DataSourcesRaster, ESRI.ArcGIS.Geodatabase, ESRI.ArcGIS.Geometry.
  2. Call this function from your code.
[VBA]
Sub ExportRasterToBytes(pRaster As IRaster)
    'This example shows how one can export a Raster to bytes.
    On Error GoTo ERH
    
    'Convert a Raster to bytes.
    Dim pConverter As IRasterExporter
    Set pConverter = New RasterExporter
    Dim pBytes() As Byte
    pBytes = pConverter.ExportToBytes(pRaster, "BMP")
    
    'Open a RasterDataset from bytes.
    Dim pWorkspace3 As IRasterWorkspace3
    Dim pNewDataset As IRasterDataset
    Set pWorkspace3 = OpenRasterWorkspace("c:\temp")
    Set pNewDataset = pWorkspace3.OpenRasterDatasetFromBytes(pBytes, True)
ERH:
    MsgBox Err.Description
End Sub

Public Function OpenRasterWorkspace(sPath As String) As IRasterWorkspace
    
    ' Create RasterWorkspace
    Dim pWsFact As IWorkspaceFactory
    Set pWsFact = New RasterWorkspaceFactory
    If pWsFact.IsWorkspace(sPath) Then
        Set createRasterWorkspace = pWsFact.OpenFromFile(sPath, 0)
    End If
End Function