/// <summary> /// Constructor /// </summary> /// <param name="gdalDataset">The gdal dataset</param> public GeoTransform(OSGeo.GDAL.Dataset gdalDataset) { if (gdalDataset == null) throw new ArgumentException("GeoTransform constructor invoked with null dataset.", "gdalDataset"); var array = new double[6]; gdalDataset.GetGeoTransform(array); _transform = array; ComputeInverse(); }
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)); }