无法使用内存设备上下文绘制位图

时间:2014-10-15 12:14:42

标签: winapi mfc

我有一个位图。我想使用Memory Devuce Context(CDC)将它显示在一个窗口中。

以下是代码段

void CMFCTest_1View::OnDraw(CDC* pDC)
{
    CMFCTest_1Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    CRect Rect;
    GetClientRect(&Rect);

    CDC MemoryDC;
    MemoryDC.CreateCompatibleDC(pDC);

    CRect MemoryRect;
    MemoryRect.left = 0;
    MemoryRect.top = 0;
    MemoryRect.right = MemoryDC.GetDeviceCaps(HORZRES);
    MemoryRect.bottom = MemoryDC.GetDeviceCaps(VERTRES);

    CBitmap bmpMem;
    CBitmap *pBmpOld;
    bmpMem.CreateCompatibleBitmap(pDC, MemoryRect.Width(), MemoryRect.Height());
    pBmpOld = MemoryDC.SelectObject(&bmpMem);


    MemoryDC.SetMapMode(MM_ISOTROPIC);
    MemoryDC.SetWindowExt( 16,16 );
    MemoryDC.SetViewportExt(MemoryRect.Width(), MemoryRect.Height());


    HGLOBAL      m_hDIB;
    CPalette*   m_Pal = new CPalette();
    BOOL bRet = LoadBMP( L"makevalue.bmp", &m_hDIB, m_Pal );


    DrawDIB( &MemoryDC, CPoint(18, 0), 18, 18, &m_hDIB, m_Pal, 1);

    pDC->StretchBlt(18,0,18,18, &MemoryDC, 0, 0, 18, 18, SRCCOPY);

    MemoryDC.SelectObject(pBmpOld);
    MemoryDC.DeleteDC();

}

StretchBit在视图上绘制黑色矩形而不是位图。以下是DrawDIB函数,它将位图绘制到Memory Device Conext

void DrawDIB( CDC* pDC, CPoint &point, int destwidth, int destheight, HGLOBAL *phDIB, CPalette *pPal , int mirrorBitmap)
{
   LPVOID  lpDIBBits;              // Pointer to DIB bits

   BITMAPINFO &bmInfo = *(LPBITMAPINFO)(*phDIB) ;
   int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed : 
                                           1 << bmInfo.bmiHeader.biBitCount;

   if( bmInfo.bmiHeader.biBitCount > 8 )
       lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
               bmInfo.bmiHeader.biClrUsed) +
               ((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
   else
       lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);

   if( pPal && (pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE) )
   {
       pDC->SelectPalette(pPal, FALSE);
       pDC->RealizePalette();
   }

   if(mirrorBitmap == 2)//Spiegelung
   {
       point.x = point.x + destwidth - 1;
       destwidth = -destwidth;
   }

  int RC = ::StretchDIBits(
                    pDC->m_hDC,                     // handle to device context
                    point.x,                        // x-coordinate of upper-left corner
                                                    //  of dest. rectangle
                    point.y,                        // y-coordinate of upper-left corner
                                                    //  of dest. rectangle
                    destwidth,                      // width of destination rectangle
                    destheight,                     // height of destination rectangle
                    0,                              // x-coordinate of upper-left corner of
                                                    //  source rectangle
                    0,                              // y-coordinate of upper-left corner of 
                                                    //  source rectangle
                    bmInfo.bmiHeader.biWidth,       // width of source rectangle
                    bmInfo.bmiHeader.biHeight,      // height of source rectangle
                    lpDIBBits,            // address of bitmap bits
                    (LPBITMAPINFO)(*phDIB),  // address of bitmap data
                    DIB_RGB_COLORS,                   // usage flags
                    SRCAND );                   // raster operation code
}

0 个答案:

没有答案