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);
}
你有什麼試過?您是否已將CBitmap對象選擇到設備上下文中? – 0x499602D2
我在第一篇文章中寫了我的代碼。 – Seer