public static GeoBitmap LoadImageInfo(string file) { using (var ds = Gdal.Open(file, GdalconstJNI.GA_ReadOnly_get())) { log.InfoFormat("Raster dataset parameters:"); log.InfoFormat(" Projection: " + ds.ProjectionRef); log.InfoFormat(" RasterCount: " + ds.RasterCount); log.InfoFormat(" RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")"); Org.Gdal.Gdal.Driver drv = ds.Driver; log.InfoFormat("Using driver " + drv.LongName); log.InfoFormat("Corner Coordinates:"); log.InfoFormat(" Upper Left (" + GDALInfoGetPosition(ds, 0.0, 0.0) + ")"); log.InfoFormat(" Lower Left (" + GDALInfoGetPosition(ds, 0.0, ds.RasterYSize) + ")"); log.InfoFormat(" Upper Right (" + GDALInfoGetPosition(ds, ds.RasterXSize, 0.0) + ")"); log.InfoFormat(" Lower Right (" + GDALInfoGetPosition(ds, ds.RasterXSize, ds.RasterYSize) + ")"); log.InfoFormat(" Center (" + GDALInfoGetPosition(ds, ds.RasterXSize / 2, ds.RasterYSize / 2) + ")"); log.InfoFormat(""); string projection = ds.ProjectionRef; if (projection != null) { SpatialReference srs = new SpatialReference(null); if (srs.ImportFromWkt(projection) == 0) { string[] wkt = new string[1]; srs.ExportToPrettyWkt(wkt, 0); log.InfoFormat("Coordinate System is:"); log.InfoFormat(wkt[0]); } else { log.InfoFormat("Coordinate System is:"); log.InfoFormat(projection); } } var TL = GDALInfoGetPositionDouble(ds, 0.0, 0.0); var BR = GDALInfoGetPositionDouble(ds, ds.RasterXSize, ds.RasterYSize); var resolution = Math.Abs(BR[0] - TL[0]) / ds.RasterXSize; if (resolution == 1) { throw new Exception("Invalid coords"); } return(new GeoBitmap(file, resolution, ds.RasterXSize, ds.RasterYSize, TL[0], TL[1], BR[0], BR[1])); } }
public static Bitmap LoadImage(string file) { lock (locker) { using (var ds = Gdal.Open(file, GdalconstJNI.GA_ReadOnly_get())) { // 8bit geotiff - single band if (ds.RasterCount == 1) { Band band = ds.GetRasterBand(1); if (band == null) { return(null); } ColorTable ct = band.GetRasterColorTable(); // Create a Bitmap to store the GDAL image in Bitmap bitmap = new Bitmap(ds.RasterXSize, ds.RasterYSize, SkiaSharp.SKColorType.Gray8); // Obtaining the bitmap buffer BitmapData bitmapData = bitmap.LockBits(0, 0, ds.RasterXSize, ds.RasterYSize, ImageLockMode.ReadWrite, SkiaSharp.SKColorType.Rgba8888); try { if (ct != null) { int iCol = ct.GetCount(); //ColorPalette pal = bitmap.Palette; for (int i = 0; i < iCol; i++) { ColorEntry ce = ct.GetColorEntry(i); //pal.Entries[i] = Color.FromArgb(ce.c4, ce.c1, ce.c2, ce.c3); } //bitmap.Palette = pal; } else { } int stride = bitmapData.Stride; IntPtr buf = bitmapData.Scan0; var buffer = new byte[ds.RasterXSize * ds.RasterYSize]; band.ReadRaster(0, 0, ds.RasterXSize, ds.RasterYSize, ds.RasterXSize, ds.RasterYSize, (int)DataType.GDT_Byte, buffer); Marshal.Copy(buffer, 0, buf, buffer.Length); } finally { bitmap.UnlockBits(bitmapData); } return(bitmap); } { Bitmap bitmap = new Bitmap(ds.RasterXSize, ds.RasterYSize, PixelFormat.Format32bppArgb); if (ds.RasterCount == 3) { // when we load a 24bit bitmap, we need to set the alpha channel else we get nothing using (var tmp = Graphics.FromImage(bitmap)) { tmp.Clear(Color.White); } } for (int a = 1; a <= ds.RasterCount; a++) { // Get the GDAL Band objects from the Dataset Band band = ds.GetRasterBand(a); if (band == null) { return(null); } var cint = (ColorInterp)band.GetColorInterpretation(); // Obtaining the bitmap buffer BitmapData bitmapData = bitmap.LockBits(0, 0, ds.RasterXSize, ds.RasterYSize, ImageLockMode.ReadWrite, SkiaSharp.SKColorType.Rgba8888); try { int stride = bitmapData.Stride; IntPtr buf = bitmapData.Scan0; var buffer = new byte[ds.RasterXSize * ds.RasterYSize]; band.ReadRaster(0, 0, ds.RasterXSize, ds.RasterYSize, ds.RasterXSize, ds.RasterYSize, (int)DataType.GDT_Byte, buffer); int c = 0; if (cint == ColorInterp.GCI_AlphaBand) { foreach (var b in buffer) { Marshal.WriteByte(buf, 3 + c * 4, (byte)b); c++; } } else if (cint == ColorInterp.GCI_RedBand) { foreach (var b in buffer) { Marshal.WriteByte(buf, 2 + c * 4, (byte)b); c++; } } else if (cint == ColorInterp.GCI_GreenBand) { foreach (var b in buffer) { Marshal.WriteByte(buf, 1 + c * 4, (byte)b); c++; } } else if (cint == ColorInterp.GCI_BlueBand) { foreach (var b in buffer) { Marshal.WriteByte(buf, 0 + c * 4, (byte)b); c++; } } else { } } finally { bitmap.UnlockBits(bitmapData); } } //bitmap.Save("gdal.png", ImageFormat.Png); return(bitmap); } } } return(null); }