我有两个位图:Gdiplus::Bitmap *pbmBitmap, pbmBitmap1;它们包含两个图像。我如何将它们合并为一张图像?我正在尝试这样的事情:Bitmap* dstBitmap = new Bitmap(pbmBitmap->GetWidth(), pbmBitmap->GetHeight() + pbmBitmap1->GetHeight()); //create dst bitmapHDC dcmem = CreateCompatibleDC(NULL);SelectObject(dcmem, pbmBitmap); //select first bitmapHDC dcmemDst = CreateCompatibleDC(NULL);SelectObject(dcmem1, dstBitmap ); //select destination bitmapBitBlt(dcmemDst em1, 0, 0, pbmBitmap->GetWidth(), pbmBitmap->GetHeight(), dcmem, 0, 0, SRCCOPY); //copy first bitmap into destination bitmapHBITMAP 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::BitmapHDC dcDestination = CreateCompatibleDC(NULL); //create device contex for our destination bitmapHBITMAP HBitmapDestination = CreateCompatibleBitmap(dcDestination, pbmBitmap->GetWidth(), pbmBitmap->GetHeight()); //create HBITMAP with correct sizeSelectObject(dcDestination, dcDestination); //select created hbitmap on our destination dcHDC dcSource = CreateCompatibleDC(NULL); //create device contex for our source bitmapSelectObject(dcSource, bitmapSource); //select source bitmap on our source dcBitBlt(dcDestination, 0, 0, pbmBitmap->GetWidth(), pbmBitmap->GetHeight(), dcSource, 0, 0, SRCCOPY); //copy piece of bitmap with correct sizeSaveBitmap(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是我的自定义函数,当我尝试保存源位图时它可以工作。为什么我不能将一个位图复制到另一个? 最佳答案 使用Graphics对象将它们组合在一起。这是一些示例工作代码...当然,您应该使用诸如unique_ptr 之类的智能ptr来代替new / delete,但是我不想假设您使用的是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;}主要功能int _tmain(int argc, _TCHAR* argv[]){ ULONG_PTR token; GdiplusStartupInput input; GdiplusStartup(&token, &input, nullptr); CombineImages(); GdiplusShutdown(token); return 0;}关于c++ - 将两个Gdiplus::Bitmap合并到一个C++中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17796849/
10-11 15:06