中国地图
GIS探索者
地图网
GIS论坛
网站首页| GIS论坛 |新闻动态 | 技术专栏 | GIS书籍 | 资源下载 | 求职招聘 | 研究生考试 | GIS博客 | GIS空间站
欢迎光临GIS空间站!   网站地图
  专栏导航:| ArcGIS | MapInfo | TopMap | 地理信息 | 遥感 | GPS | 测绘 | WebGIS | LBS | 前沿 | 数据库 | 开发 | 行业应用
  推荐栏目:| AO开发 | 业界动态 | GIS资源 | 招聘信息 | 求职简历 | 共享软件 | 电子书籍 | 研究生考题 | 电子期刊 | GIS论文
您现在的位置: GIS空间站 >> 技术专栏 >> ArcGIS >> AO开发 >> 文章正文

生成单波段栅格图像(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;
        }

文章录入:3S_Studio    责任编辑:3S_Studio  发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
■■相 关 图 书:
■■相 关 文 章:
  • 生成3波段Raster图像(C#+Engine)

  • 上一篇文章:

  • 下一篇文章: 没有了
  •  
    GIS博客精华
    TopMap下载
    热点文章
    热门资源
     
    关于我们 | 联系我们 | 广告服务 | 友情链接 | 申请链接 | 合作联盟 | 诚聘英才
      GIS空间站(GISSky.Net)版权所有 站长:兔八哥