首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图像从CDC传输到CBitmap

将图像从CDC传输到CBitmap
EN

Stack Overflow用户
提问于 2013-07-02 01:39:47
回答 1查看 5.6K关注 0票数 2

如何将图像从疾病预防控制中心传送到CBitmap?问题是:我在CBitmap A中有一个很大的图像。我需要将这个图像的一部分转换成一个向量存储,因为我不能使用一些CBitmap来实现:)我把一个准备好的疾病控制中心变成一个循环(获得CBitmap A的一个必要部分),然后我需要将它传输到CBitmap x。我怎么做呢?

这是我的密码:

代码语言:javascript
复制
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);

//没关系-我有一张原始照片

代码语言:javascript
复制
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);

//我这里有黑色方块!-以前的代码有问题

代码语言:javascript
复制
        m_bitmaps.push_back(destBitmap);

        destDC.SelectObject(oldBitmap);
    }

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

//和后来的CBitmaps是黑色方格

,我找到了解决办法!

解析CBitmap并初始化向量

代码语言:javascript
复制
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();

绘图:

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

发布于 2013-07-02 02:37:36

创建另一个设备上下文,逐个创建并选择目标位图,并使用BitBlt将源位图的部分复制到其中。下面是你如何做到这一点的一个例子。

代码语言:javascript
复制
// 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17416152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档