private SharpMap.Geometries.BoundingBox GetExtent() { if (_GdalDataset != null) { double [] geoTrans = new double[6]; _GdalDataset.GetGeoTransform(geoTrans); GeoTransform GT = new GeoTransform(geoTrans); return(new SharpMap.Geometries.BoundingBox(GT.Left, GT.Top + (GT.VerticalPixelResolution * _GdalDataset.RasterYSize), GT.Left + (GT.HorizontalPixelResolution * _GdalDataset.RasterXSize), GT.Top)); } return(null); }
private void GetPreview(GDAL.Dataset dataset, System.Drawing.Size size, Graphics g, SharpMap.Geometries.BoundingBox 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.Left, _Envelope.Left); double top = Math.Min(bbox.Top, _Envelope.Top); double right = Math.Min(bbox.Right, _Envelope.Right); double bottom = Math.Max(bbox.Bottom, _Envelope.Bottom); 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]; 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; if (band.GetRasterColorInterpretation() == 5) { ch = 0; } if (band.GetRasterColorInterpretation() == 4) { ch = 1; } if (band.GetRasterColorInterpretation() == 3) { ch = 2; } if (band.GetRasterColorInterpretation() != 2) { 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)); }