C#+AE9.2生成多波段栅格数据功能函数

C3+Ae9.2写的生成多波段栅格数据的函数

参数的实例为:

                    //savepath = "C:\\tmpdata\\";
                    //savename = "testfile.img";

                    //FYIR3FileInfo 为我保存的数据。

具体到其他使用要修改的,这个是生成四波段的,如果用的不是四波段,要修改里面的波段数据。

生成的效果是可以的,既可以在ae的mapcontrol控件中显示,也可以输出为img格式的文件。

         struct RasterInfo
        {
            public int lNumbands;
        }
        /// <summary>  
        /// 生成多波段IR3L栅格文件数据功能  
        /// </summary>  
        /// <param name="FYIR3FileInfo"></param>
        /// <param name="savepath"></param> 
        /// <param name="savefilename"></param>
        /// <returns></returns> 
        public ILayer CreateThreeBandIR3L(IR3LFile FYIR3FileInfo, string savepath, string savefilename)
        {
            //The file must not be there. Otherwise, dataset can not be got.
            if (File.Exists(savepath + savefilename))
            {
                MessageBox.Show("File Exist!");
                IRasterLayer pRasterLayer = new RasterLayerClass();
                pRasterLayer.CreateFromFilePath(savepath + savefilename);
                ILayer pLayer;
                pLayer = pRasterLayer;
                pLayer.Name = "New Raster";

                return pLayer;
            }
            // 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(FYIR3FileInfo.dblLeftDownLong, FYIR3FileInfo.dblLeftDownLat);
                // Create the dataset  
                IRasterWorkspace2 rasterWorkspace2 = null;
                double xPixelSize;
                double yPixelSize;
                //像元大小
                xPixelSize = (FYIR3FileInfo.dblcenterLong - FYIR3FileInfo.dblLeftDownLong) / (FYIR3FileInfo.intNumX / 2);
                yPixelSize = (FYIR3FileInfo.dblcenterLat - FYIR3FileInfo.dblLeftDownLat) / (FYIR3FileInfo.intNumY / 2);

                //坐标系
                ISpatialReferenceFactory2 spatRefFact = new SpatialReferenceEnvironmentClass();
                //m_ProjectedCoordinateSystem = spatRefFact.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_49N);
                ESRI.ArcGIS.Geometry.IGeographicCoordinateSystem m_ProjectedCoordinateSystem;
                m_ProjectedCoordinateSystem = spatRefFact.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
                rasterWorkspace2 = createRasterWorkspace(savepath);
                rasterDataset = rasterWorkspace2.CreateRasterDataset(savefilename, "IMAGINE Image", originPoint, FYIR3FileInfo.intNumX, FYIR3FileInfo.intNumY, xPixelSize, yPixelSize, 4, rstPixelType.PT_UCHAR, m_ProjectedCoordinateSystem, true);//rstPixelType.PT_UCHAR,new UnknownCoordinateSystemClass()

                RasterInfo rstInfo = new RasterInfo();
                rstInfo.lNumbands = 4;

                IRawPixels[] rawPixels = new IRawPixels[rstInfo.lNumbands];
                IPixelBlock3[] pixelBlock3 = new IPixelBlock3[rstInfo.lNumbands];
                IPnt pixelBlockOrigin = null;
                IPnt pixelBlockSize = null;
                IRasterBandCollection rasterBandCollection;
                IRasterProps rasterProps;
                // QI for IRawPixels and IRasterProps   
                rasterBandCollection = (IRasterBandCollection)rasterDataset;

                for (int i = 0; i < rstInfo.lNumbands; i ++ )
                {
                    rawPixels[i] = (IRawPixels)rasterBandCollection.Item(i);
                }

                //调用调色板
                string PalFilePath;
                CreateIMG.Class.Clsfengyun.Pal TmppalInfo;
                TmppalInfo = new CreateIMG.Class.Clsfengyun.Pal();
                PalFilePath = Application.StartupPath + "\\系统文件\\pal\\I-01.pal";
                ReadPal(PalFilePath, ref TmppalInfo);

                int tmpColorValue;
                System.Array[] pixelData = new System.Array[rstInfo.lNumbands];

                rasterProps = (IRasterProps)rawPixels[0];
                for (int i = 0; i < rstInfo.lNumbands; i++)
                {
                    rasterProps = (IRasterProps)rawPixels[i];
                    // Create pixelblock   
                    pixelBlockOrigin = new DblPntClass();
                    pixelBlockOrigin.SetCoords(0, 0);
                    pixelBlockSize = new DblPntClass();
                    pixelBlockSize.SetCoords(rasterProps.Width, rasterProps.Height);

                    pixelBlock3[i] = (IPixelBlock3)rawPixels[i].CreatePixelBlock(pixelBlockSize);
                    // Read pixelblock   
                    rawPixels[i].Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3[i]);
                    // Get pixeldata array    
                    pixelData[i] = (System.Array)pixelBlock3[i].get_PixelDataByRef(0);

                    for (int ii = 0; ii < rasterProps.Width; ii++)
                        for (int jj = 0; jj < rasterProps.Height; jj++)
                        {
                            if (i == 0)
                            {
                                tmpColorValue = TmppalInfo.PalColor[FYIR3FileInfo.data[ii, jj]].tmpR;
                                pixelData[i].SetValue(Convert.ToByte(tmpColorValue), ii, jj);
                            }
                            else if (i == 1)
                            {
                                tmpColorValue = TmppalInfo.PalColor[FYIR3FileInfo.data[ii, jj]].tmpG;
                                pixelData[i].SetValue(Convert.ToByte(tmpColorValue), ii, jj);
                            }
                            else if (i == 2)
                            {
                                tmpColorValue = TmppalInfo.PalColor[FYIR3FileInfo.data[ii, jj]].tmpB;
                                pixelData[i].SetValue(Convert.ToByte(tmpColorValue), ii, jj);
                            }
                            else
                            {
                                tmpColorValue = TmppalInfo.PalColor[FYIR3FileInfo.data[ii, jj]].tmpValue;
                                pixelData[i].SetValue(Convert.ToByte(tmpColorValue), ii, jj);
                            }
                        }
                    pixelBlock3[i].set_PixelData(0, (System.Object)pixelData[i]);

                    System.Object cachePointer;
                    cachePointer = rawPixels[i].AcquireCache();
                    rawPixels[i].Write(pixelBlockOrigin, (IPixelBlock)pixelBlock3[i]);
                    rawPixels[i].ReturnCache(cachePointer);
                }

                IRasterLayer pRasterLayer = new RasterLayerClass();
                pRasterLayer.CreateFromDataset(rasterDataset);
                ILayer pLayer;
                pLayer = pRasterLayer;
                pLayer.Name = "New Raster";

                return pLayer;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                //System.Diagnostics.Debug.WriteLine(ex.Message);
                return null;
            }
        }

原文地址:https://www.cnblogs.com/yuxuetaoxp/p/1716300.html