C++ 将两个Gdiplus::Bitmap合并为一个c++;
我有两个位图:C++ 将两个Gdiplus::Bitmap合并为一个c++;,c++,bitmap,merge,gdi+,C++,Bitmap,Merge,Gdi+,我有两个位图: Gdiplus::Bitmap *pbmBitmap, pbmBitmap1; 它们包含两个图像。如何将它们合并到一个图像中 我试过这样的方法: Bitmap* dstBitmap = new Bitmap(pbmBitmap->GetWidth(), pbmBitmap->GetHeight() + pbmBitmap1->GetHeight()); //create dst bitmap HDC dcmem = CreateCompatibleDC(NU
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是我的自定义函数,当我尝试保存源位图时,它可以工作。
为什么我不能将一个位图复制到另一个???使用图形对象来组合它们。下面是一些示例工作代码。。。当然,您应该使用像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;
}
下面是我自己的函数,它接收图像文件的矢量并将它们进行特别合并 wstring组合位图(矢量文件) { wstring新文件{L“Output.png”}
Gdiplus::Status rc;
CLSID-pngClsid;
GetEncoderClsid(L“图像/png”和pngClsid);
向量bmpSrc;
整数高度{0};
对于(int i=0;iGetHeight();
}
Gdiplus::Bitmap dstp位图(bmpSrc[0]->GetWidth(),Height);
Gdiplus::Graphics*g=Gdiplus::Graphics::FromImage(&dstbmap);
对于(int i=0;iDrawImage(bmpSrc[i],0,i*bmpSrc[i]->GetHeight();
}
rc=dstBitmap.Save(NewFile.c_str(),&pngClsid,NULL);
删除g;
返回新文件;
}
您可以将其称为如下所示:
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);
矢量屏幕;
屏幕。向后推(L“屏幕快照1”);
屏幕。向后推(L“屏幕快照2”);
屏幕。向后推(L“屏幕快照3”);
屏幕。向后推(L“屏幕快照4”);
屏幕。向后推(L“屏幕快照5”);
组合贴图(屏幕);
您在哪里加载原始位图?您是指pbmBitmap和pbmBitmap?我一开始就把它们装上。它们加载正确。我用save检查了它。我做这类事情已经有一段时间了,但我建议检查所有操作的结果(BitBlt和SelectObject返回非零,dcMem和dcMemDst不为NULL)看看我的编辑。我检查了每一个电话。SelectObject正在返回NOTNULL,所有dc也不为NULL。BitBlt正在返回TRUE。
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);