我需要創建一個由glReadPixels返回的數據HBITMAP)一個HBITMAP(撥打:創建從glReadPixels
HDC hCompDC = CreateCompatibleDC(NULL);
HDC hDC = GetDC();
m_hClipboardBitmap = CreateCompatibleBitmap(hDC, size.cx, size.cy);
if (m_hClipboardBitmap == NULL)
{
throw runtime_error("Unable to create bitmap.");
}
HBITMAP hOldBm = (HBITMAP) SelectObject(hCompDC, m_hClipboardBitmap);
int numberOfBytes = 4 * size.cx * size.cy;
unsigned char *pPixelData = new unsigned char[numberOfBytes];
::glReadPixels(minimum.x, minimum.y, size.cx, size.cy, GL_BGRA, GL_UNSIGNED_BYTE, pPixelData);
我試着使用:
BITMAPINFOHEADER header;
header.biWidth = size.cx;
header.biHeight = size.cy;
header.biSizeImage = numberOfBytes;
header.biSize = sizeof(BITMAPINFOHEADER);
header.biPlanes = 1;
header.biBitCount = 4 * 8; // RGBA
header.biCompression = 0;
header.biXPelsPerMeter = 0;
header.biYPelsPerMeter = 0;
header.biClrUsed = 0;
header.biClrImportant = 0;
HANDLE handle = (HANDLE)::GlobalAlloc (GHND, sizeof(BITMAPINFOHEADER) + numberOfBytes);
if(handle != NULL)
{
char *pData = (char *) ::GlobalLock((HGLOBAL)handle);
memcpy(pData,&header,sizeof(BITMAPINFOHEADER));
memcpy(pData + sizeof(BITMAPINFOHEADER), pPixelData, numberOfBytes);
::GlobalUnlock((HGLOBAL)handle);
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_DIB, handle);
CloseClipboard();
}
這粘貼到MSPAINT OK(所以數據是好的)但是我怎麼能把它變成HBITMAP?!?!
我的GDI32技能有點生疏,但你看過[CreateDIBitmap](http://msdn.microsoft.com/en-us/library/dd183491 \(v = vs.85 \).aspx)?我認爲它應該只是'CreateDIBitmap(hDC,&header,CBM_INIT,pPixelData,&info,DIB_RGB_COLORS)' – user786653
我已經嘗試過使用CreateDIBitmap,但它不適用於我。我結束了使用BITMAPFILEHEADER和BITMAPINFOHEADER並直接寫入到.bmp。如果我能夠獲得HBITMAP,但沒有想到會更好。回到C#它是... – user907285