在AO里如何使用C#生成单波段栅格图像代码
- 组件式GIS
- 2008-01-28
- 98热度
- 0评论
看了下面的代码,就知道了在AO里如何使用C#生成单波段栅格图像,其实并不难.
IRasterDataset createFileRasterDataset(string directoryName, string fileName)
{
//The file must not be there. Otherwise, dataset can not be got.
if (File.Exists(directoryName+fileName))
{
MessageBox.Show("File Exist!");
IRasterLayer pRasterLayer = new RasterLayerClass();
pRasterLayer.CreateFromFilePath(directoryName + fileName);
ILayer pLayer;
pLayer = pRasterLayer;
pLayer.Name = "New Raster";
axMapControl1.Map.AddLayer(pLayer);
axMapControl1.ActiveView.Refresh();
return null;
}
// This function creates a new img file in the given workspace
// and then assigns pixel values
try
{
IRasterDataset rasterDataset = null;
IPoint originPoint = new PointClass();
originPoint.PutCoords(0, 0);
// Create the dataset
IRasterWorkspace2 rasterWorkspace2 = null;
// IGeographicCoordinateSystem m_GeographicCoordinateSystem;
IProjectedCoordinateSystem m_ProjectedCoordinateSystem;
ISpatialReferenceFactory2 spatRefFact = new SpatialReferenceEnvironmentClass();
m_ProjectedCoordinateSystem = spatRefFact.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_49N);
rasterWorkspace2 = createRasterWorkspace(directoryName);
rasterDataset = rasterWorkspace2.CreateRasterDataset(fileName, "IMAGINE Image", originPoint, 200, 100, 1, 1, 1, rstPixelType.PT_UCHAR, m_ProjectedCoordinateSystem, true);//rstPixelType.PT_UCHAR,new UnknownCoordinateSystemClass()
IRawPixels rawPixels = null;
IPixelBlock3 pixelBlock3 = null;
IPnt pixelBlockOrigin = null;
IPnt pixelBlockSize = null;
IRasterBandCollection rasterBandCollection;
IRasterProps rasterProps;
// QI for IRawPixels and IRasterProps
rasterBandCollection = (IRasterBandCollection)rasterDataset;
rawPixels = (IRawPixels)rasterBandCollection.Item(0);
rasterProps = (IRasterProps)rawPixels;
// Create pixelblock
pixelBlockOrigin = new DblPntClass();
pixelBlockOrigin.SetCoords(0, 0);
pixelBlockSize = new DblPntClass();
pixelBlockSize.SetCoords(rasterProps.Width, rasterProps.Height);
pixelBlock3 = (IPixelBlock3)rawPixels.CreatePixelBlock(pixelBlockSize);
// Read pixelblock
rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3);
// Get pixeldata array
System.Array pixelData;
pixelData = (System.Array)pixelBlock3.get_PixelDataByRef(0);
// Loop through all the pixels and assign value
for (int i = 0; i < rasterProps.Width; i++)
for (int j = 0; j < rasterProps.Height; j++)
pixelData.SetValue(Convert.ToByte((i * j) % 255), i, j);
pixelBlock3.set_PixelData(0, (System.Object)pixelData);
// Write the pixeldata back
System.Object cachePointer;
cachePointer = rawPixels.AcquireCache();
rawPixels.Write(pixelBlockOrigin, (IPixelBlock)pixelBlock3);
rawPixels.ReturnCache(cachePointer);
IRasterLayer pRasterLayer = new RasterLayerClass();
pRasterLayer.CreateFromDataset(rasterDataset);
ILayer pLayer;
pLayer = pRasterLayer;
pLayer.Name = "New Raster";
axMapControl1.Map.AddLayer(pLayer);
axMapControl1.ActiveView.Refresh();
return rasterDataset;
// userDataset= rasterDataset;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return null;
}
}
public IRasterWorkspace2 createRasterWorkspace(string pathName)
{
// Create RasterWorkspace
IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
return workspaceFactory.OpenFromFile(pathName, 0) as IRasterWorkspace2;
}