コード例 #1
0
ファイル: Form1.cs プロジェクト: ismethr/GSAL4Test
        /// <summary>
        /// GDAL栅格转换为位图
        /// </summary>
        /// <param name="ds">GDAL Dataset</param>
        /// <param name="showRect">显示区域</param>
        /// <param name="bandlist">需要显示的波段列表</param>
        /// <returns>返回Bitmap对象</returns>
        public Bitmap GetImage(OSGeo.GDAL.Dataset ds, Rectangle showRect, int[] bandlist)
        {
            int imgWidth = ds.RasterXSize;   //影像宽
            int imgHeight = ds.RasterYSize;  //影像高

            float ImgRatio = imgWidth / (float)imgHeight;  //影像宽高比

            //获取显示控件大小
            int BoxWidth = showRect.Width;
            int BoxHeight = showRect.Height;

            float BoxRatio = imgWidth / (float)imgHeight;  //显示控件宽高比

            //计算实际显示区域大小,防止影像畸变显示
            int BufferWidth, BufferHeight;
            if (BoxRatio >= ImgRatio)
            {
                BufferHeight = BoxHeight;
                BufferWidth = (int)(BoxHeight * ImgRatio);
            }
            else
            {
                BufferWidth = BoxWidth;
                BufferHeight = (int)(BoxWidth / ImgRatio);
            }

            //构建位图
            Bitmap bitmap = new Bitmap(BufferWidth, BufferHeight,
                                     System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            //if (bandlist.Length == 3)     //RGB显示
            //{

            //    int[] r = new int[BufferWidth * BufferHeight];
            //    Band band1 = ds.GetRasterBand(bandlist[0]);
            //    band1.ReadRaster(0, 0, imgWidth, imgHeight, r, BufferWidth, BufferHeight, 0, 0);  //读取图像到内存

            //    //为了显示好看,进行最大最小值拉伸显示
            //    double[] maxandmin1 = { 0, 0 };
            //    band1.ComputeRasterMinMax(maxandmin1, 0);

            //    int[] g = new int[BufferWidth * BufferHeight];
            //    Band band2 = ds.GetRasterBand(bandlist[1]);
            //    band2.ReadRaster(0, 0, imgWidth, imgHeight, g, BufferWidth, BufferHeight, 0, 0);

            //    double[] maxandmin2 = { 0, 0 };
            //    band2.ComputeRasterMinMax(maxandmin2, 0);

            //    int[] b = new int[BufferWidth * BufferHeight];
            //    Band band3 = ds.GetRasterBand(bandlist[2]);
            //    band3.ReadRaster(0, 0, imgWidth, imgHeight, b, BufferWidth, BufferHeight, 0, 0);

            //    double[] maxandmin3 = { 0, 0 };
            //    band3.ComputeRasterMinMax(maxandmin3, 0);

            //    int i, j;
            //    for (i = 0; i < BufferWidth; i++)
            //    {
            //        for (j = 0; j < BufferHeight; j++)
            //        {
            //            int rVal = Convert.ToInt32(r[i + j * BufferWidth]);
            //            rVal = (int)((rVal - maxandmin1[0]) / (maxandmin1[1] - maxandmin1[0]) * 255);

            //            int gVal = Convert.ToInt32(g[i + j * BufferWidth]);
            //            gVal = (int)((gVal - maxandmin2[0]) / (maxandmin2[1] - maxandmin2[0]) * 255);

            //            int bVal = Convert.ToInt32(b[i + j * BufferWidth]);
            //            bVal = (int)((bVal - maxandmin3[0]) / (maxandmin3[1] - maxandmin3[0]) * 255);

            //            Color newColor = Color.FromArgb(rVal, gVal, bVal);
            //            bitmap.SetPixel(i, j, newColor);
            //        }
            //    }
            //}
            //else               //灰度显示
            {
                int[] r = new int[BufferWidth * BufferHeight];
                Band band1 = ds.GetRasterBand(bandlist[0]);
                band1.ReadRaster(0, 0, imgWidth, imgHeight, r, BufferWidth, BufferHeight, 0, 0);

                double[] maxandmin1 = { 0, 0 };
                band1.ComputeRasterMinMax(maxandmin1, 0);

                int i, j;
                for (i = 0; i < BufferWidth; i++)
                {
                    for (j = 0; j < BufferHeight; j++)
                    {
                        int rVal = Convert.ToInt32(r[i + j * BufferWidth]);
                        rVal = (int)((rVal - maxandmin1[0]) / (maxandmin1[1] - maxandmin1[0]) * 255);

                        Color newColor = Color.FromArgb(rVal, rVal, rVal);
                        bitmap.SetPixel(i, j, newColor);
                    }
                }
            }

            return bitmap;
        }
コード例 #2
0
ファイル: GdalRasterLayer.cs プロジェクト: lishxi/_SharpMap
        private void GetPreview(OSGeo.GDAL.Dataset dataset, System.Drawing.Size size, Graphics g, IEnvelope bbox)
        {
            double[] geoTrans = new double[6];
            dataset.GetGeoTransform(geoTrans);
            GeoTransform GT = new GeoTransform(geoTrans);

            int DsWidth = dataset.RasterXSize;
            int DsHeight = dataset.RasterYSize;

            Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
            int iPixelSize = 3; //Format24bppRgb = byte[b,g,r]

            if (dataset != null)
            {
                /*
                if ((float)size.Width / (float)size.Height > (float)DsWidth / (float)DsHeight)
                    size.Width = size.Height * DsWidth / DsHeight;
                else
                    size.Height = size.Width * DsHeight / DsWidth;
                */


                double left = Math.Max(bbox.MinX, _Envelope.MinX);
                double top = Math.Min(bbox.MaxY, _Envelope.MaxY);
                double right = Math.Min(bbox.MaxX, _Envelope.MaxX);
                double bottom = Math.Max(bbox.MinY, _Envelope.MinY);


                int x1 = (int)GT.PixelX(left);
                int y1 = (int)GT.PixelY(top);
                int x1width = (int)GT.PixelXwidth(right - left);

                int y1height = (int)GT.PixelYwidth(bottom - top);


                bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
                BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, size.Width, size.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);

                try
                {
                    unsafe
                    {
                        for (int i = 1; i <= (dataset.RasterCount > 3 ? 3 : dataset.RasterCount); ++i)
                        {
                            byte[] buffer = new byte[size.Width * size.Height];
                            OSGeo.GDAL.Band band = dataset.GetRasterBand(i);

                            //band.ReadRaster(x1, y1, x1width, y1height, buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution);
                            band.ReadRaster(x1, y1, x1width, y1height, buffer, size.Width, size.Height, 0, 0);

                            int p_indx = 0;
                            int ch = 0;

                            //#warning Check correspondance between enum and integer values
                            if (band.GetRasterColorInterpretation() == OSGeo.GDAL.ColorInterp.GCI_BlueBand) ch = 0;
                            if (band.GetRasterColorInterpretation() == OSGeo.GDAL.ColorInterp.GCI_GreenBand) ch = 1;
                            if (band.GetRasterColorInterpretation() == OSGeo.GDAL.ColorInterp.GCI_RedBand) ch = 2;
                            if (band.GetRasterColorInterpretation() != OSGeo.GDAL.ColorInterp.GCI_PaletteIndex)
                            {
                                for (int y = 0; y < size.Height; y++)
                                {
                                    byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
                                    for (int x = 0; x < size.Width; x++, p_indx++)
                                    {
                                        row[x * iPixelSize + ch] = buffer[p_indx];
                                    }
                                }
                            }
                            else //8bit Grayscale
                            {
                                for (int y = 0; y < size.Height; y++)
                                {
                                    byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
                                    for (int x = 0; x < size.Width; x++, p_indx++)
                                    {
                                        row[x * iPixelSize] = buffer[p_indx];
                                        row[x * iPixelSize + 1] = buffer[p_indx];
                                        row[x * iPixelSize + 2] = buffer[p_indx];
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    bitmap.UnlockBits(bitmapData);
                }
            }
            g.DrawImage(bitmap, new System.Drawing.Point(0, 0));
        }