1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

problem selecting a bitmap into printer DC

Discussion in 'MFC' started by hlin_do, Feb 9, 2007.

  1. hlin_do

    hlin_do New Member

    Joined:
    Feb 8, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I have a problem to SelectObject of Bitmap into memery DC. My code is like,
    Code:
    HINSTANCE hinstOriginal;
    hinstOriginal = ::AfxGetResourceHandle();
    ::AfxSetResourceHandle(g_hResInstance);
    
    CDC mem;
    CBitmap *oldBmp;
    CBitmap temp;
    BOOL B = temp.LoadBitmap(id);   //id is resource id 
    BITMAP BitMap;
    temp.GetBitmap(&BitMap);
    
    mem.CreateCompatibleDC( pPrintDC );             // a printer DC
    mem.SetMapMode (pPrintDC->GetMapMode ());
    
    oldBmp = mem.SelectObject(&temp);       // this call returns 0 on some PCs! 
    
    CPoint size;
    size.x = BitMap.bmWidth;
    size.y = BitMap.bmHeight;
    mem.DPtoLP(&size);
    
    BOOL bret = pGDoc->m_pDC->StretchBlt(m_iX,
                    m_iY*-1,
                    m_iWidth,
                    m_iHeight*-1, 
                    &mem,
                    0,
                    0,
                    size.x,
                    size.y,
                    SRCCOPY);
    This code works on same PC, but not on others, SelectObject returns 0;
    Please help.

    Thank you very much.

    hlin_do

    henryzlin@hotmail.com
     
    Last edited by a moderator: Feb 10, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    Check out the error code with the GetLastError.
     
  3. hlin_do

    hlin_do New Member

    Joined:
    Feb 8, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    GetLastError returns 0 befaore and after SelectObject call

    hlin_do
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    I see a definite problem here

    BITMAP BitMap;

    Variable is declared but not initialized

    temp.GetBitmap(&BitMap);

    GetBitmap should not work to the variable which is not initilaized

    oldBmp = mem.SelectObject(&temp);

    Obvious reason is it should fail

    On some systems the garbage for BitMap may not be NULL.
     
  5. hlin_do

    hlin_do New Member

    Joined:
    Feb 8, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I think I know how to make it work now. Basicly, I need to load as DIB instead bitmap.

    Thanks anyway!

    hlin_do
     

Share This Page