private static void SaveBitmapDirect(string filename, Dataset ds, int xOff, int yOff, int width, int height, int imageWidth, int imageHeight) { if (ds.RasterCount == 0) return; int[] bandMap = new int[4] { 1, 1, 1, 1 }; int channelCount = 1; bool hasAlpha = false; bool isIndexed = false; int channelSize = 8; ColorTable ct = null; // Evaluate the bands and find out a proper image transfer format for (int i = 0; i < ds.RasterCount; i++) { Band band = ds.GetRasterBand(i + 1); if (Gdal.GetDataTypeSize(band.DataType) > 8) channelSize = 16; switch (band.GetRasterColorInterpretation()) { case ColorInterp.GCI_AlphaBand: channelCount = 4; hasAlpha = true; bandMap[3] = i + 1; break; case ColorInterp.GCI_BlueBand: if (channelCount < 3) channelCount = 3; bandMap[0] = i + 1; break; case ColorInterp.GCI_RedBand: if (channelCount < 3) channelCount = 3; bandMap[2] = i + 1; break; case ColorInterp.GCI_GreenBand: if (channelCount < 3) channelCount = 3; bandMap[1] = i + 1; break; case ColorInterp.GCI_PaletteIndex: ct = band.GetRasterColorTable(); isIndexed = true; bandMap[0] = i + 1; break; case ColorInterp.GCI_GrayIndex: isIndexed = true; bandMap[0] = i + 1; break; default: // we create the bandmap using the dataset ordering by default if (i < 4 && bandMap[i] == 0) { if (channelCount < i) channelCount = i; bandMap[i] = i + 1; } break; } } // find out the pixel format based on the gathered information PixelFormat pixelFormat; DataType dataType; int pixelSpace; if (isIndexed) { pixelFormat = PixelFormat.Format8bppIndexed; dataType = DataType.GDT_Byte; pixelSpace = 1; } else { if (channelCount == 1) { if (channelSize > 8) { pixelFormat = PixelFormat.Format16bppGrayScale; dataType = DataType.GDT_Int16; pixelSpace = 2; } else { pixelFormat = PixelFormat.Format24bppRgb; channelCount = 3; dataType = DataType.GDT_Byte; pixelSpace = 3; } } else { if (hasAlpha) { if (channelSize > 8) { pixelFormat = PixelFormat.Format64bppArgb; dataType = DataType.GDT_UInt16; pixelSpace = 8; } else { pixelFormat = PixelFormat.Format32bppArgb; dataType = DataType.GDT_Byte; pixelSpace = 4; } channelCount = 4; } else { if (channelSize > 8) { pixelFormat = PixelFormat.Format48bppRgb; dataType = DataType.GDT_UInt16; pixelSpace = 6; } else { pixelFormat = PixelFormat.Format24bppRgb; dataType = DataType.GDT_Byte; pixelSpace = 3; } channelCount = 3; } } } // Create a Bitmap to store the GDAL image in Bitmap bitmap = new Bitmap(imageWidth, imageHeight, pixelFormat); if (isIndexed) { // setting up the color table 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 { // grayscale ColorPalette pal = bitmap.Palette; for (int i = 0; i < 256; i++) pal.Entries[i] = Color.FromArgb(255, i, i, i); bitmap.Palette = pal; } } // Use GDAL raster reading methods to read the image data directly into the Bitmap BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadWrite, pixelFormat); try { int stride = bitmapData.Stride; IntPtr buf = bitmapData.Scan0; ds.ReadRaster(xOff, yOff, width, height, buf, imageWidth, imageHeight, dataType, channelCount, bandMap, pixelSpace, stride, 1); } finally { bitmap.UnlockBits(bitmapData); } bitmap.Save(filename); }
private static void LoadBitmapDirect(Dataset ds, Bitmap bitmap, int xOff, int yOff, int width, int height, int imageWidth, int imageHeight, int iOverview) { if (isIndexed) { // setting up the color table 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 { // grayscale ColorPalette pal = bitmap.Palette; for (int i = 0; i < 256; i++) pal.Entries[i] = Color.FromArgb(255, i, i, i); bitmap.Palette = pal; } } // Use GDAL raster reading methods to read the image data directly into the Bitmap BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, imageWidth, imageHeight), ImageLockMode.ReadWrite, pixelFormat); try { int stride = bitmapData.Stride; IntPtr buf = bitmapData.Scan0; ds.ReadRaster(xOff, yOff, width, height, buf, imageWidth, imageHeight, dataType, channelCount, bandMap, pixelSpace, stride, 1); } finally { bitmap.UnlockBits(bitmapData); } }