[gdal-dev] Converting .NET Bitmap to GDAL Dataset

Maksim Sestic max at geoinova.com
Sun Mar 24 15:41:48 PDT 2013


Hi Tamas,

 

Yes, finally, "vsimem" works once bitmap is Png (abbreviated VB.NET code
follows):

 

      Dim bmp As Bitmap = Bitmap.FromFile("C:\Input.tif")

 

            Dim imageBuffer As Byte()

            Using stream As New MemoryStream()

                bmp.Save(stream, ImageFormat.Png)

                imageBuffer = stream.ToArray()

            End Using

 

            Dim pinnedArray As GCHandle = GCHandle.Alloc(imageBuffer,
GCHandleType.Pinned)

            Dim pointer As IntPtr = pinnedArray.AddrOfPinnedObject()

 

            Dim memFilename As String = "/vsimem/data"

            Gdal.FileFromMemBuffer(memFilename, imageBuffer.Length, pointer)

            Dim ds As Dataset = Gdal.Open(memFilename, Access.GA_ReadOnly)

 

            Dim drv As Driver = Gdal.GetDriverByName("GTiff")

            Dim ds1 = drv.CreateCopy("C:\Output.tif", ds, 0, Nothing,
Nothing, Nothing)

 

            pinnedArray.Free()

 

I've tested it on bitmaps originating from .bmp, .png, .tif and .jpg.

 

Regards,

Maksim Sestic

 

  _____  

From: Tamas Szekeres [mailto:szekerest at gmail.com] 
Sent: Sunday, March 24, 2013 17:25
To: Maksim Sestic
Cc: gdal-dev
Subject: Re: [gdal-dev] Converting .NET Bitmap to GDAL Dataset

 

We could also convert the bitmap to png or some other formats that the GDAL
drivers reconise, something like:

 

        byte[] imageBuffer;

        using (MemoryStream stream = new MemoryStream())

        {

            bmp.Save(stream, ImageFormat.Png);

            imageBuffer = stream.ToArray();

        }

 

        Gdal.FileFromMemBuffer(memFilename, imageBuffer);

        Dataset ds = Gdal.Open(memFilename, Access.GA_ReadOnly);

 

 

Best regards,

 

Tamas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20130324/04118b91/attachment.html>


More information about the gdal-dev mailing list