将两个Gdiplus :: Bitmap合并为一个c ++

时间:2013-07-22 20:32:35

标签: c++ bitmap merge gdi+

我有两个位图:

Gdiplus::Bitmap *pbmBitmap, pbmBitmap1;

它们包含两个图像。我如何将它们合并为一个图像?

我正在尝试这样的事情:

Bitmap* dstBitmap = new Bitmap(pbmBitmap->GetWidth(), pbmBitmap->GetHeight() + pbmBitmap1->GetHeight()); //create dst bitmap

HDC dcmem = CreateCompatibleDC(NULL);
SelectObject(dcmem, pbmBitmap); //select first bitmap

HDC dcmemDst = CreateCompatibleDC(NULL);
SelectObject(dcmem1, dstBitmap ); //select destination bitmap

BitBlt(dcmemDst em1, 0, 0, pbmBitmap->GetWidth(), pbmBitmap->GetHeight(), dcmem, 0, 0, SRCCOPY); //copy first bitmap into destination bitmap

HBITMAP CreatedBitmap = CreateCompatibleBitmap(dcmem, pbmBitmap->GetWidth(), pbmBitmap->GetHeight() + pbmBitmap1->GetHeight());

dstBitmap = new Bitmap(CreatedBitmap, NULL);
dstBitmap ->Save(L"omg.bmp", &pngClsid, 0); //pngClsid i took from msdn

我知道 - 丑陋的代码,但我需要在C ++中完成它。 我变黑了。为什么呢?

//修改

经过两个小时的谷歌搜索和阅读后,我得到了这个:

HBITMAP bitmapSource;
pbmBitmap->GetHBITMAP(Color::White, &bitmapSource); //create HBITMAP from Gdiplus::Bitmap

HDC dcDestination = CreateCompatibleDC(NULL); //create device contex for our destination bitmap
HBITMAP HBitmapDestination = CreateCompatibleBitmap(dcDestination, pbmBitmap->GetWidth(), pbmBitmap->GetHeight()); //create HBITMAP with correct size
SelectObject(dcDestination, dcDestination); //select created hbitmap on our destination dc

HDC dcSource = CreateCompatibleDC(NULL); //create device contex for our source bitmap
SelectObject(dcSource, bitmapSource); //select source bitmap on our source dc

BitBlt(dcDestination, 0, 0, pbmBitmap->GetWidth(), pbmBitmap->GetHeight(), dcSource, 0, 0, SRCCOPY); //copy piece of bitmap with correct size

SaveBitmap(dcDestination, HBitmapDestination, "OMG.bmp"); //not working i get 24kb bitmap
//SaveBitmap(dcSource, bitmapSource, "OMG.bmp"); //works like a boss, so it's problem with SaveBitmap function

它应该工作,但我得到24kb位图。 SaveBitmap是我的自定义函数,它在我尝试保存源位图时有效。 为什么我不能将一个位图复制到另一个位图?

2 个答案:

答案 0 :(得分:2)

使用Graphics对象组合它们。这是一些示例工作代码...当然,你应该使用像unique_ptr<>这样的智能ptrs而不是新的/删除,但我不想假设你使用VS 2012或更新。

void CombineImages()
{
    Status rc;

    CLSID pngClsid;
    GetEncoderClsid(L"image/png", &pngClsid);

    Bitmap* bmpSrc1 = Bitmap::FromFile(L"Image1.JPG");
    assert(bmpSrc1->GetLastStatus() == Ok);
    Bitmap* bmpSrc2 = Bitmap::FromFile(L"Image2.JPG");
    assert(bmpSrc2->GetLastStatus() == Ok);

    Bitmap dstBitmap(bmpSrc1->GetWidth(), bmpSrc1->GetHeight() + bmpSrc2->GetHeight());
    assert(dstBitmap.GetLastStatus() == Ok);

    Graphics* g = Graphics::FromImage(&dstBitmap);
    rc = g->DrawImage(bmpSrc1, 0, 0 );
    assert(rc == Ok);
    rc = g->DrawImage(bmpSrc2, 0, bmpSrc1->GetHeight());
    assert(rc == Ok);

    rc = dstBitmap.Save(L"Output.png", &pngClsid, NULL);
    assert(rc == Ok);

    delete g;
    delete bmpSrc1;
    delete bmpSrc2;
}

主要fn ..

int _tmain(int argc, _TCHAR* argv[])
{
    ULONG_PTR token;
    GdiplusStartupInput input;

    GdiplusStartup(&token, &input, nullptr);
    CombineImages();
    GdiplusShutdown(token);
    return 0;
}

答案 1 :(得分:0)

这是我自己的函数,它接收图像文件的矢量并进行垂直合并。

wstring CombineBitmaps(矢量文件) { wstring NewFile{ L"Output.png" };

Gdiplus::Status rc;

CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);

vector< Gdiplus::Bitmap*> bmpSrc;
int Height{ 0 };
for (int i = 0; i < files.size(); i++)
{
    bmpSrc.push_back (Gdiplus::Bitmap::FromFile(files[i].c_str()));
    Height += bmpSrc[0]->GetHeight();
} 
Gdiplus::Bitmap dstBitmap(bmpSrc[0]->GetWidth(), Height);
Gdiplus::Graphics* g = Gdiplus::Graphics::FromImage(&dstBitmap);
for (int i = 0; i < files.size(); i++)
{
    rc = g->DrawImage(bmpSrc[i], 0, i*bmpSrc[i]->GetHeight());
}

rc = dstBitmap.Save(NewFile.c_str(), &pngClsid, NULL);

delete g;

return NewFile;

}

您可以按如下方式调用它:

vector<wstring> screens;
screens.push_back(L"screenshot1");
screens.push_back(L"screenshot2");
screens.push_back(L"screenshot3");
screens.push_back(L"screenshot4");
screens.push_back(L"screenshot5");
CombineBitmaps(screens);