Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Returning CBitmap* pointer

Returning CBitmap* pointer

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdebugginghelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    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|-858993460

    Why ? It is not the same pBitmap object ? Could you help me ? Kindly thank for any further help !

    L 1 Reply Last reply
    0
    • _ _Flaviu

      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|-858993460

      Why ? It is not the same pBitmap object ? Could you help me ? Kindly thank for any further help !

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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 do pOldBitmap and pOldBmp come from?

      	CBitmap\* pFinalBitmap = MemDC.SelectObject(pOldBitmap);
      	SourceDC.SelectObject(pOldBmp);
      	pBitmap = new CBitmap;
      	pBitmap->Attach((HBITMAP)pFinalBitmap->GetSafeHandle());
      
      _ 1 Reply Last reply
      0
      • L Lost User

        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 do pOldBitmap and pOldBmp come from?

        	CBitmap\* pFinalBitmap = MemDC.SelectObject(pOldBitmap);
        	SourceDC.SelectObject(pOldBmp);
        	pBitmap = new CBitmap;
        	pBitmap->Attach((HBITMAP)pFinalBitmap->GetSafeHandle());
        
        _ Offline
        _ Offline
        _Flaviu
        wrote on last edited by
        #3

        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;

        L 1 Reply Last reply
        0
        • _ _Flaviu

          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;

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Well, that is quite different from the code in your original question.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups