This sample shows how to create a file raster dataset, set NoData and write pixel values.
How to use
- Reference libraries: ESRI.ArcGIS.DataSourcesRaster, ESRI.ArcGIS.Geodatabase, ESRI.ArcGIS.Geometry.
- Call this function from your code.
Sub CreateFileRasterDataset()
'open a raster workspace
Dim pWs As IRasterWorkspace2
Set pWs = OpenRasterWorkspace("c:\temp")
'set the origin, width and height of the raster dataset
Dim pOrigin As IPoint
Dim Width As Long
Dim Height As Long
Set pOrigin = New Point
pOrigin.PutCoords 15, 15
Width = 100
Height = 100
'Create the raster dataset
Dim pRasterDs As IRasterDataset
Set pRasterDs = pWs.CreateRasterDataset("myimage1.img", "IMAGINE Image", pOrigin, Width, Height, 30, 30, 3, PT_UCHAR, New UnknownCoordinateSystem, True)
Dim pProp As IRasterProps
Dim pBands As IRasterBandCollection
Dim nBands As Integer
Dim pBand As IRasterBand
'Set NoData if necessary
Set pBands = pRasterDs
nBands = pBands.Count
For k = 0 To nBands - 1
Set pBand = pBands.Item(k)
Set pProp = pBand
pProp.NoDataValue = 255
Next k
'Create a Raster from the raster dataset
Dim pRaster As IRaster
Set pRaster = pRasterDs.CreateDefaultRaster
'Create a pixel block for the pixels that need to write
Dim pPB As IPixelBlock3
Dim pPnt As IPnt
Set pPnt = New Pnt
pPnt.SetCoords Width, Height
Set pPB = pRaster.CreatePixelBlock(pPnt)
Dim v As Variant
For k = 0 To nBands - 1
v = pPB.PixelData(k)
For i = 0 To Width - 1
For j = 0 To Height - 1
If i = j Then v(i, j) = 255 Else v(i, j) = (i + j) / 8 'set some values that are in 8 bit range
Next j
Next i
pPB.PixelData(k) = v
Set v = Nothing
Next k
'Write the pixel block with a (0,0) offset of the upper left corner
Dim pRasterEdit As IRasterEdit
Set pRasterEdit = pRaster
pPnt.SetCoords 0, 0
pRasterEdit.Write pPnt, pPB
'clean up
Set pRasterEdit = Nothing
Set pRaster = Nothing
Set pProp = Nothing
Set pPB = Nothing
Set pBand = Nothing
Set pBands = Nothing
Set pRasterDs = Nothing
Set pWs = Nothing
End Sub
Public Function OpenRasterWorkspace(sPath As String) As IRasterWorkspace2
' Create RasterWorkspace
Dim pWsFact As IWorkspaceFactory
Set pWsFact = New RasterWorkspaceFactory
If pWsFact.IsWorkspace(sPath) Then
Set OpenRasterWorkspace = pWsFact.OpenFromFile(sPath, 0)
End If
Set pWsFact = Nothing
End Function