[Gdal-dev] Read one band image to a bitmap in csharp

Tamas Szekeres szekerest at gmail.com
Mon Mar 12 19:33:10 EDT 2007


Johan,

I seems that  bitmap.Palette retrieves the array by value not by
reference, so you should copy the palette as the following code:

            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;

I've updated GDALReadDirect.cs to support this kind of images.


Best regards,

Tamas


2007/3/12, Hallgren Johan E <jhhal at wmdata.com>:
> Hello again
>
> I tried some palette examination but it was not so successful.
>
> I used the following code, but the colors were wrong. Everything was
> more or less blue.
> **********************
> Band band = ds.GetRasterBand(1);
>
> ColorTable ct = band.GetRasterColorTable();
> if (ct == null)
> {
>         Console.WriteLine("   Band has no color table!");
>         return null;
> }
>
> if (ct.GetPaletteInterpretation() != PaletteInterp.GPI_RGB)
> {
>         Console.WriteLine("   Only RGB palette interp is supported by
> this sample!");
>         return null;
> }
>
> // Get the width and height of the Dataset
> int width = ds.RasterXSize;
> int height = ds.RasterYSize;
>
> // Create a Bitmap to store the GDAL image in
> Bitmap bitmap = new Bitmap(width, height,
> PixelFormat.Format8bppIndexed);
>
> // Use GDAL raster reading methods to read the image data directly into
> the Bitmap
> BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width,
> height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
>
> try
> {
>         int iCol = ct.GetCount();
>         ColorEntry ce = new ColorEntry();
>         for (int i = 0; i < iCol; i++)
>         {
>                 int a = ct.GetColorEntryAsRGB(i, ce);
>                 bitmap.Palette.Entries[i] = Color.FromArgb(ce.c4, ce.c1,
> ce.c2, ce.c3);
>         }
>
>         int stride = bitmapData.Stride;
>         IntPtr buf = bitmapData.Scan0;
>
>         band.ReadRaster(0, 0, width, height, buf, width, height,
> DataType.GDT_Byte, 1, stride);
>
> }
> finally
> {
>         bitmap.UnlockBits(bitmapData);
> }
>
> **********************
> I hope you have time to have a look at the code.
>
> :)
> Johan
> ___________________________________
>
>
> Johan Hallgren
>
>
> WM-data a LogicaCMG company
> Pelle Bergs backe 3
> Box 1938, 791 19 Falun
> Tel: 023-547 46 (int: +46-2354746)
>
> Mobil: 070-588 44 28 (int: +46-705884428)
>
> johan.e.hallgren at wmdata.com
> http://www.wmdata.se
>
> -----Original Message-----
> From: Tamas Szekeres [mailto:szekerest at gmail.com]
> Sent: den 12 mars 2007 16:50
> To: Hallgren Johan E
> Subject: Re: [Gdal-dev] Read one band image to a bitmap in csharp
>
> 2007/3/12, Hallgren Johan E <jhhal at wmdata.com>:
> > Thanks Tamas,
> >
> > Now things working! The code below is from the GDALRead.cs sample and
> it
> > seams to be a bit slow. Is it possible to use the
> BitmapData/LockBitmap
> > instead and would it be faster?
> >
>
> Hopefully, creating a .net image with PixelFormat = 8bppIndexed would
> be faster, I haven't tested it though. It is possible to use
> BitmapData/LockBitmap directly, GDALReadDirect.cs uses this approach.
> In this case the palette should also be copied into the .net image.
>
> >
> > Regaring the Metadata and image extent.
> >
> > Does GDal provide any support for coordinate information stored in a
> > world file? Or does I have to read the file myself. I hoped that GDal
> > provided me with a uniform way of reading geographic information
> > wherever it's stored.
> >
>
> I think DataSet.GetGeoTransform could retrieve the transormation
> parameters as an array of floats (array length is 6)
>
>
> Best regards,
>
> Tamas
>



More information about the Gdal-dev mailing list