Exemplo n.º 1
0
        /// <summary>
        /// Sets the color table for a raster band.
        /// Only works in GTiff of <see cref="Byte"/> or <see cref="UInt16"/>
        /// </summary>
        /// <param name="band"></param>
        public static void SetColorTable(Band band)
        {
            var ct     = new ColorTable(PaletteInterp.GPI_RGB);
            var colors = new Color[] {
                Color.FromArgb(150, 163, 255, 115),
                Color.FromArgb(150, 38, 115, 0),
                Color.FromArgb(150, 76, 230, 0),
                Color.FromArgb(150, 112, 168, 0),
                Color.FromArgb(150, 0, 92, 255),
                Color.FromArgb(150, 197, 0, 255),
                Color.FromArgb(150, 255, 170, 0),
                Color.FromArgb(150, 0, 255, 197),
                Color.FromArgb(150, 255, 255, 255)
            };

            var i = 10;

            foreach (var c in colors)
            {
                var ce = new ColorEntry();
                ce.c4 = c.A;
                ce.c3 = c.B;
                ce.c2 = c.G;
                ce.c1 = c.R;
                ct.SetColorEntry(i, ce);
                i += 10;
            }
            band.SetRasterColorTable(ct);
        }
Exemplo n.º 2
0
        public static Bitmap LoadImage(string file)
        {
            lock (locker)
            {
                using (var ds = OSGeo.GDAL.Gdal.Open(file, OSGeo.GDAL.Access.GA_ReadOnly))
                {
                    // Get the GDAL Band objects from the Dataset
                    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, PixelFormat.Format8bppIndexed);
                    // Obtaining the bitmap buffer
                    BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, ds.RasterXSize, ds.RasterYSize),
                                                            ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
                    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;
                        }

                        int    stride = bitmapData.Stride;
                        IntPtr buf    = bitmapData.Scan0;

                        band.ReadRaster(0, 0, ds.RasterXSize, ds.RasterYSize, buf, ds.RasterXSize, ds.RasterYSize,
                                        DataType.GDT_Byte, 1, stride);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }

                    return(bitmap);
                }
            }
        }
Exemplo n.º 3
0
        public static Bitmap LoadImage(string file)
        {
            lock (locker)
            {
                using (var ds = OSGeo.GDAL.Gdal.Open(file, OSGeo.GDAL.Access.GA_ReadOnly))
                {
                    // 8bit geotiff - single band
                    if (ds.RasterCount == 1)
                    {
                        Band band = ds.GetRasterBand(1);
                        if (band == null)
                        {
                            return(null);
                        }

                        ColorTable ct = band.GetRasterColorTable();

                        PixelFormat format = PixelFormat.Format8bppIndexed;

                        // Create a Bitmap to store the GDAL image in
                        Bitmap bitmap = new Bitmap(ds.RasterXSize, ds.RasterYSize, format);

                        // Obtaining the bitmap buffer
                        BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, ds.RasterXSize, ds.RasterYSize),
                                                                ImageLockMode.ReadWrite, format);
                        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;

                            band.ReadRaster(0, 0, ds.RasterXSize, ds.RasterYSize, buf, ds.RasterXSize, ds.RasterYSize,
                                            DataType.GDT_Byte, 1, stride);
                        }
                        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 = band.GetColorInterpretation();


                            // Obtaining the bitmap buffer
                            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, ds.RasterXSize, ds.RasterYSize),
                                                                    ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                            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, buffer, ds.RasterXSize,
                                                ds.RasterYSize, 1, ds.RasterXSize);

                                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);
        }