AW: [gdal-dev] Create gdaldataset from in-memory Image/Bitmap/byte[]/MemoryStream

Tamas Szekeres szekerest at
Wed May 26 17:22:27 EDT 2010

2010/5/26 Even Rouault <even.rouault at>

> Felix,
> yes you should be able to use FileFromMemBuffer() to create a virtual file
> and
> open it with the PNG driver (since the PNG driver supports virtual IO -->
> as
> confirmed by gdalinfo --format PNG)
> Here's the C API for it :
> And here's an extract of a test in the Python autotest suite.
>    content = open('data/byte.tif', mode='rb').read()
>    # Create in-memory file and initialize it with the content
>    gdal.FileFromMemBuffer('/vsimem/tiffinmem', content)
>    # Open the in-memory file
>    ds = gdal.Open('/vsimem/tiffinmem')
>    # do something with the dataset
>    # Close the dataset
>    ds = None
>    # Release memory associated to the in-memory file
>    gdal.Unlink('/vsimem/tiffinmem')
> Disclaimer: You should be able to adapt the above for C#, but I haven't
> tested
> with the C# bindings, so there's a risk that they aren't ready if there's
> some missing required typemap.

The current version of the C# signature of FileFromMemBuffer is rude and not
user friendly, though I could imagine the following transcript of the python
code above for the C# bindings (not tested):

byte[] bytes = File.ReadAllBytes("data\\byte.tif");
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);

    using (Dataset ds = gdal.Open("/vsimem/tiffinmem"))
    // do something

In the future I'm about to add a second signature to the bindings  "*public
static void FileFromMemBuffer(string pszFilename, int nBytes, byte[]
pabyData)*" which could also avoid the extra memcopy implemented in the
current version.

Feel free to add a ticket <> to require
this addition.

Best regards,

-------------- next part --------------
An HTML attachment was scrubbed...

More information about the gdal-dev mailing list