2

如何将图像从 CDC 传输到 CBitmap?整个问题:我在 CBitmap A 中有一个大图像。我需要将此图像的一部分传输到多个 CBitmap 以存储到矢量中,因为我不能为此使用多个 CDC :) 我制作一个准备好的CDC进入一个循环(获取CBitmap A的必要部分)然后我需要将它转移到CBitmap x。我该怎么做?

这是我的代码:

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);
}
4

1 回答 1

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在适当的位置使用它,例如析构函数。

于 2013-07-02T02:37:36.433 回答