[Gdal-dev] C# Bindings

Tamas Szekeres szekerest at gmail.com
Wed Nov 8 18:11:31 EST 2006

> Right. So, for now, let's leave out the unsafe methods then. At some
> point, I might do some speed comparisons between the IntPtr version and
> the typesafe version. If there's a significant speed penalty (which I
> doubt) then we can revisit the idea of adding optional unsafe/fast methods.


I've added some of the typesafe overloads so you can make the comparison easily.
I've tested the code with the following sample:

        // 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.Format32bppRgb);

        double[] r = new double[width * height];
        double[] g = new double[width * height];
        double[] b = new double[width * height];

        // Get the GDAL Band objects from the Dataset
        Band redBand = ds.GetRasterBand(1);
        Band greenBand = ds.GetRasterBand(2);
        Band blueBand = ds.GetRasterBand(3);

        redBand.ReadRaster(0, 0, width, height, r, width, height, 0, 0);
        greenBand.ReadRaster(0, 0, width, height, g, width, height, 0, 0);
        blueBand.ReadRaster(0, 0, width, height, b, width, height, 0, 0);

        int i, j;
        for (i = 0; i< width; i++)
            for (j=0; j<height; j++)
                Color newColor =
                bitmap.SetPixel(i, j, newColor);


At this point I'm a bit uncertain about the proper handling of the
following types:


And also I couldn't ignore a serious bug in the SWIG default
implementation caused access violations and I've made a temporary fix
exclusively for the GDAL C# binding.
However it must be noted that this issue exists for some of the other
bindings like Java as well. It have involved the need of
reimplementing how the SWIG wrapper classes handle the internal C
memory segments.

Did you mean that the buffer offset parameter should be added only to
the IntPtr overloads?

Best Regards,


More information about the Gdal-dev mailing list