http://www.gissky.net- GIS空间站

我要投稿 投稿指南 RSS订阅 网站资讯通告:
搜索: 您现在的位置: GIS空间站 >> 技术专栏 >> ArcGIS >> ArcObjects开发 >> 正文

生成单波段栅格图像(C#+Engine9.2)

作者:ijustfu    文章来源:www.3sstudio.cn/bbs    点击数:    更新时间:2007-12-7
摘要:
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;
        }

Tags:ArcGIS Engine C#  
责任编辑:3S_Studio
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 中国地图