Re: CBitmap to IPIcture.... How?
You may try this MFC code I developed (I tested it and seems OK)
<code>
void CTestBitmapPicDlg::OnBnClickedSave()
{
// Load a bitmap
CBitmap bmp;
VERIFY( bmp.LoadBitmap( IDB_CAT ) );
// Result
HRESULT hr;
//
// Create the IPicture from the bitmap handle
//
IPicture * pPicture = NULL;
PICTDESC pictureDesc;
::ZeroMemory( &pictureDesc, sizeof(pictureDesc) );
pictureDesc.cbSizeofstruct = sizeof(pictureDesc);
pictureDesc.picType = PICTYPE_BITMAP;
pictureDesc.bmp.hbitmap = static_cast<HBITMAP>( bmp );
hr = ::OleCreatePictureIndirect( &pictureDesc, IID_IPicture, FALSE,
reinterpret_cast<void **>(&pPicture) );
if ( FAILED(hr) )
{
AfxMessageBox( _T("Error in creating picture from bitmap.") );
return;
}
//
// Create an output stream (file) to write the bitmap
//
IStream * pStream = NULL;
CString filename( _T("MyCat.bmp") );
if ( FAILED( SHCreateStreamOnFile(
filename,
STGM_WRITE | STGM_SHARE_DENY_WRITE | STGM_CREATE | STGM_DIRECT,
&pStream ) ) )
{
AfxMessageBox( _T("Error in creating file stream.") );
pPicture->Release();
pPicture = NULL;
return;
}
// Save to file
if (FAILED( pPicture->SaveAsFile( pStream, TRUE, NULL ) ) )
{
pStream->Release();
pStream = NULL;
pPicture->Release();
pPicture = NULL;
}
//
// Object cleanup
//
pStream->Release();
pStream = NULL;
pPicture->Release();
pPicture = NULL;
AfxMessageBox( _T("Bitmap saved to file.") );
}
</code>
Note that using ATL COM smart pointers, you can make your cleanup code
easier.
Giovanni
"Jessica" <Jessica@discussions.microsoft.com> ha scritto nel messaggio
news:AE4B68E0-A47A-46E5-835F-838EB0A9D85D@microsoft.com...
Hello All,
I am having a CBitMap object, and I need to create an IPicture object from
it. The code which I 've written for that is shown below. Can anyone tell
me
whether or not it's correct?
HBITMAP hBmp = (HBITMAP) bmp; // bmp is CBitmap object.
HRESULT hrCreated = E_FAIL;
IPicture* pIPict = NULL;
PICTDESC pictdesc;
pictdesc.bmp.hbitmap = hBmp;
pictdesc.picType = PICTYPE_BITMAP;
pictdesc.cbSizeofstruct = sizeof (PICTDESC);
hrCreated = OleCreatePictureIndirect(&pictdesc,
IID_IPicture,
FALSE,
(void**)&pIPict);
I tried to save the IPicute using the IPictureDisp interface, but the bmp
file created is 0 bytes. Am I missing something here?
Thanks in advance,
Jessy