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); }
public static Bitmap GetBitmap(double lng1, double lat1, double lng2, double lat2, long width, long height) { if (_cache.Count == 0) { return(null); } Bitmap output = new Bitmap((int)width, (int)height, PixelFormat.Format32bppArgb); int a = 0; using (Graphics g = Graphics.FromImage(output)) { //g.Clear(Color.Transparent); RectLatLng request = new RectLatLng(lat1, lng1, lng2 - lng1, lat2 - lat1); //g.DrawString(request.ToString(), new Font("Arial", 12), Brushes.Wheat, 0, 0); bool cleared = false; foreach (var image in _cache.ToArray()) { // calc the pixel coord within the image rect var ImageTop = (float)map(request.Top, image.Rect.Top, image.Rect.Bottom, 0, image.RasterYSize); var ImageLeft = (float)map(request.Left, image.Rect.Left, image.Rect.Right, 0, image.RasterXSize); var ImageBottom = (float)map(request.Bottom, image.Rect.Top, image.Rect.Bottom, 0, image.RasterYSize); var ImageRight = (float)map(request.Right, image.Rect.Left, image.Rect.Right, 0, image.RasterXSize); RectangleF rect = new RectangleF(ImageLeft, ImageTop, ImageRight - ImageLeft, ImageBottom - ImageTop); var res = (request.Right - request.Left) / width; if (rect.Left <= image.RasterXSize && rect.Top <= image.RasterYSize && rect.Right >= 0 && rect.Bottom >= 0) { if (!cleared) { //g.FillRectangle(Brushes.Green, rect.X, rect.Y, rect.Width, rect.Height); cleared = true; } //if (image.Resolution < (res / 3)) //continue; //Console.WriteLine("{0} <= {1} && {2} <= {3} || {4} >= {5} && {6} >= {7} ", rect.Left, image.RasterXSize, rect.Top, image.RasterYSize, rect.Right, 0, rect.Bottom, 0); try { lock (locker) { if (image.Bitmap == null) { continue; } // this is wrong g.DrawImage(image.Bitmap, 0, 0, width, height, rect.X, rect.Y, rect.Width, rect.Height, GraphicsUnit.Pixel); } a++; if (a >= 50) { return(output); } } catch (Exception ex) { log.Error(ex); //throw new Exception("Bad Image "+image.File); } } } if (a == 0) { return(null); } return(output); } }