[Gdal-dev] C# Bindings

Tamas Szekeres szekerest at gmail.com
Sat Nov 4 17:48:18 EST 2006


I've added the base implementation with the following signature into the CVS:

public int ReadRasterInternal(int xOff, int yOff, int xSize, int
ySize, IntPtr buffer, int buf_xSize, int buf_ySize, int buf_type, int
pixelSpace, int lineSpace);

public int WriteRasterInternal(int xOff, int yOff, int xSize, int
ySize, IntPtr buffer, int buf_xSize, int buf_ySize, int buf_type, int
pixelSpace, int lineSpace);

And tested the code with the following sample:

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

            // 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(ds.RasterXSize, ds.RasterYSize,

            // 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,

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

                blueBand.ReadRasterInternal(0, 0, width, height, buf,
width, height, 1, 4, stride);
                greenBand.ReadRasterInternal(0, 0, width, height, new
IntPtr(buf.ToInt32()+1), width, height, 1, 4, stride);
                redBand.ReadRasterInternal(0, 0, width, height, new
IntPtr(buf.ToInt32()+2), width, height, 1, 4, stride);
            finally {


At this point I would avoid creating /unsafe code into the interface
so I will leave the user alone with the pointers and the fixed
However I would add the specializations depending on the array types
using Marshal.Copy
between the managed and the unmanaged memory.

How about supporting ReadBlock/WriteBlock? Would it be required?

According to your questions:

> - For any functions that use an array as a buffer, it would be
> convenient to add a bufferOffset int argument that says at what point in
> the buffer reading/writing should begin. This allows people to use GDAL
> to read into arrays that contain pixels in "packed" format. This only
> needs to appear in the overload of these functions that has the
> pixelSpace and lineSpace args, since packed format buffers require these
> to be specified as well.

Would you describe an expected signature and a test sample for this
issue? At which side of the interface should we implement this? I
guess at the C side it would be easier.

> - How are we dealing with errors? The original GDAL functions return
> CPLErr. Are we going to do the same? Or are we going to throw exceptions?

Both of them is available. The function returns CPLErr and throws
exception on error. This behaviour is similar for most of the
interface functions.

Best Regards,


PS: I've got your attachment in the last email.

More information about the Gdal-dev mailing list