生成3波段Raster图像代码 C#+Engine
- 组件式GIS
- 2008-01-28
- 58热度
- 0评论
昨天,一同事问我怎么用C#生成3波段Raster图像,这里把代码写出来.目前只能生成3个波段。当波段大于3时会出问题。不知道为什么,那位高手知道告诉俺一声。
public void CreateMultBandsRaster(string directoryName)//, string szFile, string szRasFormat /*= "IMAGINE Image"*/
{
Random random=new Random ();
if (File.Exists("d:\vge.img"))
{
File.Delete("d:\vge.img");
}
IWorkspaceFactory m_WorkspaceFactory = new RasterWorkspaceFactoryClass();
IRasterDataset m_RasterDataSet=null;
IPoint originPoint = new PointClass();
originPoint.PutCoords(0, 0);
IRasterWorkspace2 m_RasterWorkspace2 = m_WorkspaceFactory.OpenFromFile("d:\", 0) as IRasterWorkspace2;
IProjectedCoordinateSystem m_ProjectedCoordinateSystem;
ISpatialReferenceFactory2 spatRefFact = new SpatialReferenceEnvironmentClass();
m_ProjectedCoordinateSystem = spatRefFact.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_49N);
m_RasterWorkspace2 = createRasterWorkspace(directoryName);
m_RasterDataSet = m_RasterWorkspace2.CreateRasterDataset("vge.img", "IMAGINE Image", originPoint, 10,10, 1, 1, 6, rstPixelType.PT_UCHAR, m_ProjectedCoordinateSystem, true);
IRaster m_Raster;
RasterInfo rstInfo = new RasterInfo();
rstInfo.lNumbands = 6;
IRawPixels []m_RawPixels = new IRawPixels[rstInfo.lNumbands];
m_Raster = m_RasterDataSet.CreateDefaultRaster();
IRasterProps m_RasterProps = m_Raster as IRasterProps;
// Get RasterBand from the raster
IRasterBandCollection m_RasterBandCollection = m_Raster as IRasterBandCollection;
IPixelBlock3[] m_PixelBlock = new IPixelBlock3[rstInfo.lNumbands];
for (int i = 0; i < rstInfo.lNumbands; i++)
{
m_RawPixels[i] = (IRawPixels)m_RasterBandCollection.Item(i);
}
/* */
//目前这个设置透明色值不管用,不知道正确的方法是怎么样
/*if (bNoDataValue > 0 || bNoDataValue < 256) {
VARIANT vVal;
vVal.vt = VT_UI1;
vVal.cVal = bNoDataValue;
// ipRasProps->put_NoDataValue(vVal);
ipRasProps->put_NoDataValue(CComVariant(bNoDataValue));
}
*/// QI RawPixel interface
m_RasterProps = m_Raster as IRasterProps;
// Create a DblPnt to hold the PixelBlock size
IPnt m_PixelBlockSize=new DblPntClass();
long lWidth, lHeight;
lWidth=m_RasterProps.Width;
lHeight = m_RasterProps.Height;
m_PixelBlockSize.SetCoords(lWidth, lHeight);
// Create PixelBlock with defined size
IPnt pixelBlockOrigin = new DblPntClass();
pixelBlockOrigin.SetCoords(0, 0);
System.Array[] pixelData = new System.Array[rstInfo.lNumbands];
for (int i = 0; i < rstInfo.lNumbands; i++)
{
m_PixelBlock[i] =(IPixelBlock3)m_RawPixels[i].CreatePixelBlock(m_PixelBlockSize);//When There is more than 3 bands, error happens here.
pixelData[i] = (System.Array)m_PixelBlock[i].get_PixelDataByRef(0);
for (int ii = 0; ii < m_RasterProps.Width; ii++)
for (int jj = 0; jj < m_RasterProps.Height; jj++)
{
if (i == 0)
pixelData[i].SetValue(Convert.ToByte((ii*jj)%255), ii, jj);
else if (i == 1)
{
if((ii*jj)%255>130)
pixelData[i].SetValue(Convert.ToByte((ii * jj) % 255-130), ii, jj);//random.Next(100,110)
else
pixelData[i].SetValue(Convert.ToByte(130-(ii * jj) % 255), ii, jj);//random.Next(100,110)
}
else
pixelData[i].SetValue(Convert.ToByte(255 - (ii * jj) % 255), ii, jj);
}
m_PixelBlock[i].set_PixelData(0, (System.Object)pixelData[i]);
System.Object cachePointer;
cachePointer = m_RawPixels[i].AcquireCache();
m_RawPixels[i].Write(pixelBlockOrigin, (IPixelBlock)m_PixelBlock[i]);
m_RawPixels[i].ReturnCache(cachePointer);
}
IRasterLayer pRasterLayer = new RasterLayerClass();
pRasterLayer.CreateFromDataset(m_RasterDataSet);
ILayer pLayer;
pLayer = pRasterLayer;
pLayer.Name = "New Raster";
axMapControl1.Map.AddLayer(pLayer);
axMapControl1.ActiveView.Refresh();
}