Re: Create a CBitmap from SAFEARRAY
On 14 Mar 2007 08:43:17 -0700, dom_perron@hotmail.com wrote:
Do you know where I can find documentation on how I can call .NET
component in an MFC ActiveX using /CLR? I am working with VS2003.
Thanks
Dominique
Hi,
sorry, but I don't know how to mix C++ and .NET stuff.
(Frankly speaking, if I want to use .NET, I would use C# - a well
designed language - and not what I consider an ugly "genetic-modified"
:) C++ with managed .NET extensions: the so called "managed C++".
And if I need communication between .NET managed world and native
Win32 world, I would use simple C-interface DLLs or COM interop.
But it's just IMHO.)
However, if you have a pointer to the SAFEARRAY, and you want to use
its data, this is how you might consider to do:
<CODE>
// SAFEARRAY * psa;
HRESULT hr;
BYTE * pBytes = NULL;
// Get a pointer to the elements of the array
hr = SafeArrayAccessData( psa, (void HUGEP**)&pBytes );
if (FAILED(hr))
// Error ...
// *** Use the data pointed by pBytes ***
....
....
// Release array
SafeArrayUnaccessData( psa );
pBytes = NULL;
</CODE>
If your byte array is just a memory copy of a bitmap file, you may
consider using the following function to create the bitmap from this
data.
It's just:
HBITMAP hbmpMyImage = LoadBitmapFromMemory( pBytes );
(The routine uses ::CreateDIBSection.)
Here's the code:
<CODE>
//------------------------------------------------------------------
// Load a DIB file from memory buffer.
// Return bitmap handle on success, NULL on error
//
// by MrAsm
//------------------------------------------------------------------
HBITMAP LoadBitmapFromMemory( void * pBmpFileData )
{
// Check parameter
ASSERT( pBmpFileData != NULL );
if ( pBmpFileData == NULL )
return NULL;
// Pointer to file bytes
BYTE * pFileBytes = (BYTE *)pBmpFileData;
// DIB bits (will be set by CreateDIBSection)
BYTE * pBits = NULL;
// Access the BITMAPINFOHEADER section
BITMAPINFOHEADER * pbmih = (BITMAPINFOHEADER *)(pFileBytes +
sizeof(BITMAPFILEHEADER));
// Create the DIB
HBITMAP hBitmap = ::CreateDIBSection(
NULL, // No HDC used
(BITMAPINFO *)pbmih, // Pointer to DIB information
0, // Default
(void **)&pBits, // The function will return pointer to
// bitmap bits
NULL, // No file mapping
0 ); // No file mapping
if ( hBitmap == NULL )
return NULL;
// Get pointer to bitmap bits in original file
// (we obtain the offset from BITMAPFILEHEADER.bfOffBits field)
BITMAPFILEHEADER * pbmfh = (BITMAPFILEHEADER *)pFileBytes;
BYTE * pSourceBitmapBits = pFileBytes + pbmfh->bfOffBits;
// Copy bits
memcpy(pBits, pSourceBitmapBits, pbmih->biSizeImage);
// Return bitmap handle
return hBitmap;
}
</CODE>
Hope it helps,
MrAsm