Copying raw data into Byte[] in managed C++... 
Author Message
 Copying raw data into Byte[] in managed C++...

This is essence of the way I currently do it - pretty
slow eh ?!
Is there a way to do it more efficiently - indeed can i
make the the managed type wrap the raw memory  - that
would  be nice ;-).

unsigned char MyData[ 500000 ];
..
..
..
Byte arrBytes[] = new Byte[ 500000 ];
for( int i = 0; i < 500000; i++ )    
        arrBytes[ i ] = *( MyData + i );

The data concerned is bitmap data being marshalled into
a managed GDI+ bitmap object...        

MemoryStream *pMS = new MemoryStream( arrBytes );      

Bitmap *pBM = static_cast<Bitmap *>( Image::FromStream(
pMS ) );

While my code works, it is very slow.
Thanks in advance

Tim Cannell



Sun, 17 Apr 2005 21:09:19 GMT  
 Copying raw data into Byte[] in managed C++...
As far as I know, there's no way to directly create a managed array from
unmanaged memory. However, there are a number of ways you could speed up the
process of copying data to the managed heap.

You could pin the array and then use memcpy like so:
{
Byte btDest __gc[] = new Byte[500000]; // Managed destination array
unsigned char pSource __nogc[500000]; // Unmanaged source
Byte __pin* pDest = &btDest[0]; // Pin the destination array

// TODO: .... Populate the arrays
memcpy(pDest, pSource, 500000);

Quote:
}

Or you could play it safe and use Marshal::Copy (in the
System::Runtime::InteropServices namespace) to do it for you.


Quote:
> This is essence of the way I currently do it - pretty
> slow eh ?!
> Is there a way to do it more efficiently - indeed can i
> make the the managed type wrap the raw memory  - that
> would  be nice ;-).

> unsigned char MyData[ 500000 ];
> ..
> ..
> ..
> Byte arrBytes[] = new Byte[ 500000 ];
> for( int i = 0; i < 500000; i++ )
> arrBytes[ i ] = *( MyData + i );

> The data concerned is bitmap data being marshalled into
> a managed GDI+ bitmap object...

> MemoryStream *pMS = new MemoryStream( arrBytes );

> Bitmap *pBM = static_cast<Bitmap *>( Image::FromStream(
> pMS ) );

> While my code works, it is very slow.
> Thanks in advance

> Tim Cannell



Sun, 17 Apr 2005 22:25:41 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Byte buffer cast in Managed C++

2. Managed C++ cannot handle byte[,] in interface signature.

3. Managed C++ cannot handle byte[,] in interface signature.

4. Referencing data from unmanaged code to managed code in C++ Wrapper class

5. Standard C++ basic_ofstreams and BYTEs don't write all data properly

6. Copy Excel data into Access database using ADO for Visual C++

7. using Managed dlls from Managed C++

8. Accessing managed array of bytes

9. Converting byte arrays in managed extensions

10. Raw data into strings

11. Retrieve raw data (counter value) from windows register

12. Retrieving data from an Oracle LONG RAW column

 

 
Powered by phpBB® Forum Software