在MSDN中有如下语句
An application can select a bitmap into memory device contexts only and into only one memory device context at a time. The format of the bitmap must either be monochrome or compatible with the device context; if it is not, SelectObject returns an error
位图必须是单色的或者和设备一致,问题出来了,我怎样才能使之达到一致?比如我用LoadImage或者LoadBitmap加载了一个资源,但这个位图就是跟我的设备的格式不一致,比如一个打印机的设备dc,我并不知道该dc到底是什么格式,我应该怎么办才能符合dc要求?
100分相送解决立刻送分

解决方案 »

  1.   

    msdn里不是说了吗LoadBitmap creates a compatible bitmap of the display, which cannot be selected to a printer. To load a bitmap that you can select to a printer, call LoadImage and specify LR_CREATEDIBSECTION to create a DIB section. A DIB section can be selected to any device.
      

  2.   

    设备是24位的,也能显示8位位图啊但是创建的8位位图,就不能bitblt出来
      

  3.   

    给你一个打印位图的例子:
    /*
    //打印或者在屏幕上画位图
    //pDC 打印机或者屏幕dc指针
    iLogPixelX
    iLogPixelY
    屏幕DC的GetDeviceCaps(LOGPIXELSX)值,其中
    iLogPixelX=DC.GetDeviceCaps(LOGPIXELSX);
    iLogPixelY=DC.GetDeviceCaps(LOGPIXELSY);
    strFileName BMP文件
    */
    void DrawBMP(CDC* pDC,int iLogPixelX,int iLogPixelY,const char *strFileName)
    {
    CDC MemDC; // 内存设备环境指针,在视的整个存在过程都将存在
    CBitmap Bitmap,*pOldBmp;  
    CRect Source, Dest; // 记录源位图尺寸和最终显示尺寸
    BITMAP bm;
    if(MemDC.GetSafeHdc() == NULL)
    {
    HBITMAP hbitmap=(HBITMAP)LoadImage(0,strFileName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
    Bitmap.Attach(hbitmap);
    MemDC.CreateCompatibleDC(pDC);
    Bitmap.GetObject(sizeof(bm),&bm);
    pOldBmp=MemDC.SelectObject(&Bitmap);
    Source.top=0;
    Source.left=0;
    Source.right= bm.bmWidth;
    Source.bottom = bm.bmHeight;
    Dest = Source;
    }
    pDC->DPtoLP(&Dest);
    if(pDC->IsPrinting())
    {
    Dest.left=(int)(Dest.left*((double)pDC->GetDeviceCaps(LOGPIXELSX))/iLogPixelX);
    Dest.right=(int)(Dest.right*((double)pDC->GetDeviceCaps(LOGPIXELSX))/iLogPixelX);
    Dest.top=(int)(Dest.top*((double)pDC->GetDeviceCaps(LOGPIXELSY))/iLogPixelY);
    Dest.bottom=(int)(Dest.bottom*((double)pDC->GetDeviceCaps(LOGPIXELSY))/iLogPixelY);
    }
    pDC->StretchBlt(Dest.left, Dest.top, Dest.right, Dest.bottom,
    &MemDC, Source.left, Source.top, Source.right,Source.bottom, SRCCOPY);
    MemDC.SelectObject(pOldBmp);
    Bitmap.DeleteObject();
    MemDC.DeleteDC();
    return;
    }
      

  4.   

    给你的建议:不要光看MFC,WIN32API比MFC全的多,就象bcpl所说的,LoadBitmap是一个被LoadImage取代的函数,并且是按照显示兼容格式得到的位图,这些在MSDN的CBitmap::LoadBitmap中都没有说明,而在相应的WIN32API函数中说明的很详细