Returning CBitmap* pointer
-
I have wrote a method that load and rescale a CBitmap:
CBitmap* CMyDlg::LoadPicture(CString strFile)
{
int m_nPictureSize = 170;
CBitmap* pBitmap = NULL;
HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL, strFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if(NULL != hBitmap)
{
CBitmap Bitmap, MemBitmap;
Bitmap.Attach((HBITMAP)hBitmap);
// rescale CBItmap ....
// retrieve the final bitmap
CBitmap* pFinalBitmap = MemDC.SelectObject(pOldBitmap);
SourceDC.SelectObject(pOldBmp);
pBitmap = new CBitmap;
pBitmap->Attach((HBITMAP)pFinalBitmap->GetSafeHandle());
BITMAP bm3;
pBitmap->GetBitmap(&bm3);
TRACE("Width and height INSIDE if condition: %d|%d\n", bm3.bmWidth, bm3.bmHeight);
}
BITMAP bm4;
pBitmap->GetBitmap(&bm4);
TRACE("Width and height OUTSIDE if condition: %d|%d\n", bm4.bmWidth, bm4.bmHeight);
return pBitmap;
}I want to use this method in order to add this created CBitmap pointer to add them into CTypedCBitmapList:
CBitmap\* pImage = LoadPicture(strPath); if(NULL == pImage->GetSafeHandle()) return 0; m\_PtrArrayBitmap.Add(pImage);
Ok, but the strange thing is that pBitmap from inside of
LoadPicture
method has correct size inside of 'if' condition, and wrong size outside of 'if' condition:Width and height INSIDE if condition: 170|170
Width and height OUTSIDE if condition: -858993460|-858993460Why ? It is not the same pBitmap object ? Could you help me ? Kindly thank for any further help !
-
I have wrote a method that load and rescale a CBitmap:
CBitmap* CMyDlg::LoadPicture(CString strFile)
{
int m_nPictureSize = 170;
CBitmap* pBitmap = NULL;
HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL, strFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if(NULL != hBitmap)
{
CBitmap Bitmap, MemBitmap;
Bitmap.Attach((HBITMAP)hBitmap);
// rescale CBItmap ....
// retrieve the final bitmap
CBitmap* pFinalBitmap = MemDC.SelectObject(pOldBitmap);
SourceDC.SelectObject(pOldBmp);
pBitmap = new CBitmap;
pBitmap->Attach((HBITMAP)pFinalBitmap->GetSafeHandle());
BITMAP bm3;
pBitmap->GetBitmap(&bm3);
TRACE("Width and height INSIDE if condition: %d|%d\n", bm3.bmWidth, bm3.bmHeight);
}
BITMAP bm4;
pBitmap->GetBitmap(&bm4);
TRACE("Width and height OUTSIDE if condition: %d|%d\n", bm4.bmWidth, bm4.bmHeight);
return pBitmap;
}I want to use this method in order to add this created CBitmap pointer to add them into CTypedCBitmapList:
CBitmap\* pImage = LoadPicture(strPath); if(NULL == pImage->GetSafeHandle()) return 0; m\_PtrArrayBitmap.Add(pImage);
Ok, but the strange thing is that pBitmap from inside of
LoadPicture
method has correct size inside of 'if' condition, and wrong size outside of 'if' condition:Width and height INSIDE if condition: 170|170
Width and height OUTSIDE if condition: -858993460|-858993460Why ? It is not the same pBitmap object ? Could you help me ? Kindly thank for any further help !
It appears that the bitmap you are pointing to is a temporary object whose scope is only within the
if
block. In the following block of code where dopOldBitmap
andpOldBmp
come from?CBitmap\* pFinalBitmap = MemDC.SelectObject(pOldBitmap); SourceDC.SelectObject(pOldBmp); pBitmap = new CBitmap; pBitmap->Attach((HBITMAP)pFinalBitmap->GetSafeHandle());
-
It appears that the bitmap you are pointing to is a temporary object whose scope is only within the
if
block. In the following block of code where dopOldBitmap
andpOldBmp
come from?CBitmap\* pFinalBitmap = MemDC.SelectObject(pOldBitmap); SourceDC.SelectObject(pOldBmp); pBitmap = new CBitmap; pBitmap->Attach((HBITMAP)pFinalBitmap->GetSafeHandle());
I think I figure out why it doesn't work ... pOldBitmap and pOldBmp are decalred inside of 'if' statement:
if(NULL != hBitmap) { //// ...... // select memory bitmap into memory dc CBitmap\* pOldBitmap = MemDC.SelectObject(&MemBitmap); // create a source dc CDC SourceDC; SourceDC.CreateCompatibleDC(&cdc); // select into source dc the source bitmap CBitmap\* pOldBmp = SourceDC.SelectObject(&Bitmap); //// ...... // retrieve the final bitmap CBitmap\* pFinalBitmap = MemDC.SelectObject(pOldBitmap); SourceDC.SelectObject(pOldBmp); pBitmap = new CBitmap; pBitmap->Attach((HBITMAP)pFinalBitmap->GetSafeHandle());
BITMAP bm3;
pBitmap->GetBitmap(&bm3);
TRACE("Width and height INSIDE if condition: %d|%d\n", bm3.bmWidth, bm3.bmHeight);
}
BITMAP bm4;
pBitmap->GetBitmap(&bm4);
TRACE("Width and height OUTSIDE if condition: %d|%d\n", bm4.bmWidth, bm4.bmHeight);
return pBitmap; -
I think I figure out why it doesn't work ... pOldBitmap and pOldBmp are decalred inside of 'if' statement:
if(NULL != hBitmap) { //// ...... // select memory bitmap into memory dc CBitmap\* pOldBitmap = MemDC.SelectObject(&MemBitmap); // create a source dc CDC SourceDC; SourceDC.CreateCompatibleDC(&cdc); // select into source dc the source bitmap CBitmap\* pOldBmp = SourceDC.SelectObject(&Bitmap); //// ...... // retrieve the final bitmap CBitmap\* pFinalBitmap = MemDC.SelectObject(pOldBitmap); SourceDC.SelectObject(pOldBmp); pBitmap = new CBitmap; pBitmap->Attach((HBITMAP)pFinalBitmap->GetSafeHandle());
BITMAP bm3;
pBitmap->GetBitmap(&bm3);
TRACE("Width and height INSIDE if condition: %d|%d\n", bm3.bmWidth, bm3.bmHeight);
}
BITMAP bm4;
pBitmap->GetBitmap(&bm4);
TRACE("Width and height OUTSIDE if condition: %d|%d\n", bm4.bmWidth, bm4.bmHeight);
return pBitmap;