[Gdal-dev] Re: CreateCopy into buffer

Ivan Mjartan ivan.mjartan at geovap.cz
Fri Dec 9 03:16:20 EST 2005


Hi All,

 

At first thanks a lot for yours replies. It helps a lot. I have been finished my C++ library  and I would show you my code that createcopy sub window of image into memory. I know it is simple, but for GDAL beginners like I am it is very useful see some example. 



this part of unmanage c++ dll code

BASICGDAL_API GByte* createSubImageMemory(char *pszSrcFilename,int x, int y,int xsize,int ysize,int *Length){
    vsi_l_offset nBigLength; 
    int t;
    GByte* pabyOUTData;
    char **papszCreateOptions = NULL;
    GDALDatasetH hDataset,hOutDS;
    GDALDriverH hDriver;
    VRTDataset *poVDS;
    int nBandCount = 0;
    int i;

    GDALAllRegister();
    
    hDriver = GetGDALDriverManager()->GetDriverByName("BMP");
    hDataset = GDALOpenShared( pszSrcFilename, GA_ReadOnly );

    poVDS = new VRTDataset(xsize, ysize);

    nBandCount = GDALGetRasterCount( hDataset );

    for( i = 0; i < nBandCount; i++ )
    {
        VRTSourcedRasterBand *poVRTBand;
        GDALRasterBand *poSrcBand;
        GDALDataType eBandType;
        poSrcBand = ((GDALDataset *)hDataset)->GetRasterBand(i+1);
        eBandType = poSrcBand->GetRasterDataType();
        poVDS->AddBand( eBandType, NULL );
        poVRTBand = (VRTSourcedRasterBand *) poVDS->GetRasterBand( i+1 );
        poVRTBand->AddSimpleSource( poSrcBand,x, y, xsize, ysize,0, 0, xsize, ysize );
        poVRTBand->CopyCommonInfoFrom( poSrcBand );
    }

    VSIInstallMemFileHandler();
    hOutDS = GDALCreateCopy( hDriver,"/vsimem/out.bmp", (GDALDatasetH) poVDS,
    FALSE, NULL, 
    NULL, NULL );
    
    if( hOutDS != NULL )
    {
        GDALClose( hOutDS );
    }
    
    GDALClose( (GDALDatasetH) poVDS );
    GDALClose( hDataset );
    
    pabyOUTData=VSIGetMemFileBuffer( "/vsimem/out.bmp",&nBigLength, TRUE );
    VSIUnlink("/vsimem/out.bmp"); 
    VSICleanupFileManager ();
    *Length=(int)nBigLength;
    return pabyOUTData; 
}

and this is part of C# mange code 

[DllImport("BasicGdal.dll")]
internal static extern IntPtr createSubImageMemory(string pszSrcFilename,int x, int y,int xsize,int ysize,ref int size);

int size=0;

IntPtr zacatek; 
zacatek = BasicGdal.createSubImageMemory2(this.testFile,5000,5000,3000,3000,ref size);

byte[] buf=new byte[size];


Marshal.Copy(zacatek,buf,0,size);

System.IO.FileStream fs;
fs = new System.IO.FileStream("c:\\Projects\\GDAL\\CsharpTest\\CsharpTest\\bin\\Debug\\test.bmp",System.IO.FileMode.Create);
fs.Write(buf,0,buf.Length);
fs.Close();
Marshal.FreeCoTaskMem(zacatek);

Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20051209/652226f0/attachment.html


More information about the Gdal-dev mailing list