2013-07-02 59 views
2

如何將CDC的圖像傳輸到CBitmap? 整體問題: 我有一個很大的圖像到CBitmap答:我需要將這個圖像的部分轉移到一些CBitmap的存儲到一個向量,因爲我不能使用一些CDC爲這:) 我將一個準備好的CDC製作成一個循環(獲取CBitmap A的必需部分),然後我需要將它轉移到CBitmap x。 我該怎麼辦?將圖像從CDC傳輸到CBitmap

這裏是我的代碼:

m_bitmaps.clear(); 
m_bitmaps.reserve(4); 

CDC SourceDC, destDC; 
SourceDC.CreateCompatibleDC(pMainDC); 
destDC.CreateCompatibleDC(pMainDC); 

CBitmap bmpWhole; // bmp 200*200 
bmpWhole.LoadBitmap(IDB_TEST_BITMAP); 
SourceDC.SelectObject(&bmpWhole); 

//pMainDC->BitBlt(0,0,200,200,&SourceDC,0,0,SRCCOPY); 

//這是確定的 - 我有一個源圖像

for (int x=100; x>=0; x-=100) 
    for (int y=100; y>=0; y-=100) 
    { 
     CBitmap *destBitmap = new CBitmap(); 
     destBitmap->CreateCompatibleBitmap(&destDC, 100, 100); 

     CBitmap *oldBitmap = destDC.SelectObject(destBitmap); 

     destDC.BitBlt(0,0,100,100,&SourceDC,x,y,SRCCOPY); 

     pMainDC->BitBlt(x*1.1,y*1.1,100,100,&destDC,0,0,SRCCOPY); 

//我來到這裏的黑色方塊! - 一些錯誤之前的代碼

 m_bitmaps.push_back(destBitmap); 

     destDC.SelectObject(oldBitmap); 
    } 

bmpWhole.DeleteObject(); 
destDC.DeleteDC(); 
SourceDC.DeleteDC(); 

//後來CBitmaps是黑色方塊


我找到了解決辦法!

解析的CBitmap和初始化向量

m_bitmaps.clear(); 
m_bitmaps.reserve(4); 

CDC SourceDC, destDC; 
SourceDC.CreateCompatibleDC(pMainDC); 

CBitmap bmpWhole; 
bmpWhole.LoadBitmap(IDB_TEST_BITMAP); 
SourceDC.SelectObject(&bmpWhole); 

for (int x=100; x>=0; x-=100) 
    for (int y=100; y>=0; y-=100) 
    { 
     CImage *destImage = new CImage(); 
     destImage->Create(100, 100, 24); 

     destDC.Attach(destImage->GetDC()); 
     destDC.BitBlt(0,0,100,100,&SourceDC,x,y,SRCCOPY); 
     destDC.Detach(); 
     destImage->ReleaseDC(); 

     m_bitmaps.push_back(destImage); 
    } 

bmpWhole.DeleteObject(); 
destDC.DeleteDC(); 
SourceDC.DeleteDC(); 

抽獎:

WORD nShift=0; 
for (auto iter = m_bitmaps.begin(); iter != m_bitmaps.end(); ++iter, nShift+=110) 
{ 
    CBitmap* pBitmap = CBitmap::FromHandle((*iter)->operator HBITMAP()); 

    CDC memDC; 
    memDC.CreateCompatibleDC(pMainDC); 
    memDC.SelectObject(pBitmap); 
    pMainDC->BitBlt(nShift, 0, 100, 100, &memDC, 0, 0, SRCCOPY); 
} 
+0

你有什麼試過?您是否已將CBitmap對象選擇到設備上下文中? – 0x499602D2

+0

我在第一篇文章中寫了我的代碼。 – Seer

回答

1

創建通過彼此的設備上下文和一個創建和選擇你的目標位圖到它,並使用BitBlt將源位圖的部分複製到它們中。下面是你如何做到這一點的例子。

// Create a DC compatible with the display 
// this is used to copy FROM the source bitmap 
sourceDC.CreateDC(NULL); 
sourceDC.SelectObject(&sourceBitmap); 

// Create another device context for the destination bitmap 
destDC.CreateCompatibleDC(&sourceDC); 
for(int i = 0; i < numBitmaps; i++) 
{ 
    // Determine the x, y, sourceX, sourceY, with and height here 
    // ... 

    // create a new bitmap 
    CBitmap *destBitmap = new CBitmap(); 
    destBitmap->CreateCompatibleBitmap(&destDC, width, height); 
    // Select the bitmap into the destination device context 
    CBitmap *oldBitmap = destDC.SelectObject(destBitmap); 

    // copy the bitmap 
    destDC.BitBlt(x, y, width, height, &sourceDC, sourceX, sourceY, SRCCOPY); 

    // add it to the vector  
    bitmapVector.push_back(destBitmap); 

    // Clean up 
    destDC.SelectObject(oldBitmap); 
} 

爲了簡單起見,我省略了錯誤檢查。如果您使用的是C++ 11或Boost,我建議使用unique_ptr來管理位圖對象的生存期。否則,你需要delete它在適當的地方,如析構函數。