Cianan

导航

IRaster pRaster 转换为 Bitmap (3个波段 )

IRaster pRaster 转换为 Bitmap (3个波段 )。

 1  /// <summary>
 2         /// 
 3         /// </summary>
 4         /// <param name="pRaster"></param>
 5         /// <returns></returns>
 6         public Bitmap SaveBitmap(IRaster pRaster)
 7         {
 8             //获取图层的行列值   
 9             IRasterProps pRasterProps = pRaster as IRasterProps;
10             int Height = pRasterProps.Height;
11             int Width = pRasterProps.Width;
12 
13             //定义并初始化数组,用于存储栅格内所有像员像素值
14             double[,] PixelValue_red = new double[Height, Width];
15             double[,] PixelValue_green = new double[Height, Width];
16             double[,] PixelValue_blue = new double[Height, Width];
17             // 循环3个波段
18             for (int band = 0; band < 3; band++)
19             {
20                 IRasterBandCollection pRasterBandCollection = (IRasterBandCollection)pRaster;
21                 IRaster pTempRaster = new RasterClass();
22                 IRasterBandCollection pTempRasterBandCollection = (IRasterBandCollection)pTempRaster;
23                 IRasterBand pBand = pRasterBandCollection.Item(band);
24                 pTempRasterBandCollection.AppendBand(pBand);
25                 IRaster2 pRaster2 = pTempRaster as IRaster2;
26 
27                 //定义RasterCursor初始化,参数设为null,内部自动设置PixelBlock大小
28                 IRasterCursor pRasterCursor = pRaster2.CreateCursorEx(null);
29 
30                 //用于存储PixelBlock的长宽
31                 long blockwidth = 0;
32                 long blockheight = 0;
33 
34                 IPixelBlock3 pPixelBlock3;
35                 do
36                 {
37                     //获取Cursor的左上角坐标
38                     int left = (int)pRasterCursor.TopLeft.X;
39                     int top = (int)pRasterCursor.TopLeft.Y;
40 
41                     pPixelBlock3 = pRasterCursor.PixelBlock as IPixelBlock3;
42                     blockheight = pPixelBlock3.Height;
43                     blockwidth = pPixelBlock3.Width;
44 
45                     System.Array pixels = (System.Array)pPixelBlock3.get_PixelData(0);
46 
47                     //获取该Cursor的PixelBlock中像素的值
48                     for (int i = 0; i < blockheight; i++)
49                     {
50                         //string str = "";
51                         for (int j = 0; j < blockwidth; j++)
52                         {
53                             //一定要注意,pixels中的数组排序为[Width,Height]
54                             switch (band)
55                             {
56                                 case 0: PixelValue_red[top + i, left + j] = Convert.ToDouble(pixels.GetValue(j, i)); break;
57                                 case 1: PixelValue_green[top + i, left + j] = Convert.ToDouble(pixels.GetValue(j, i)); break;
58                                 case 2: PixelValue_blue[top + i, left + j] = Convert.ToDouble(pixels.GetValue(j, i)); break;
59                             }
60                         }
61                     }
62                 }
63                 while (pRasterCursor.Next() == true);
64             }
65            
66             Bitmap bitmap = new Bitmap(Width, Height);
67             for (int w = 0; w < Width; w++)
68             {
69                 for (int h = 0; h < Height; h++)
70                 {
71                     Color color = Color.FromArgb((int)PixelValue_red[h, w], (int)PixelValue_green[h, w], (int)PixelValue_blue[h, w]);
72                     bitmap.SetPixel(w, h, color);
73                 }
74             }
75 
76             return bitmap;
77         }

 

posted on 2018-09-07 12:06  Cianan  阅读(164)  评论(0)    收藏  举报