1

我试图将 CBitmap 转换为 DIB,但它在控制台应用程序中不起作用我如何获得设备上下文?SetDIBColorTable 和 GetDIBits 都不起作用

     BITMAP bimapInfo;
 m_bmBitmap->GetBitmap(&bimapInfo);
 HDC hScrDC = ::GetDC(NULL);
 HDC hMemDC = NULL;
 hMemDC = ::CreateCompatibleDC(hScrDC); 
 //HGDIOBJ oldbmp = ::SelectObject(hMemDC,b); 
 BITMAP bmp;
 const DWORD dwcBihSize = sizeof(BITMAPINFOHEADER);
 BITMAPINFOHEADER bi;
 //GetObject(b,sizeof(tagBITMAP),&bmp);
 long dwSize = dwcBihSize +(pow(2.0,bimapInfo.bmBitsPixel)) * sizeof(RGBQUAD) + ((bmp.bmBitsPixel * width) * height);
 LPBITMAPINFO pDIB ;
 output(dwSize);
pDIB = (LPBITMAPINFO)new BYTE[dwSize];
pDIB->bmiHeader.biSize = dwcBihSize;
pDIB->bmiHeader.biWidth = width;
pDIB->bmiHeader.biHeight = height;
pDIB->bmiHeader.biBitCount = bimapInfo.bmBitsPixel;
pDIB->bmiHeader.biPlanes = 1;
pDIB->bmiHeader.biCompression = BI_RGB;
pDIB->bmiHeader.biXPelsPerMeter = 1000;
pDIB->bmiHeader.biYPelsPerMeter = 1000;
pDIB->bmiHeader.biClrUsed = 0;
pDIB->bmiHeader.biClrImportant = 0;
LPRGBQUAD lpColors =(LPRGBQUAD)(pDIB+pDIB->bmiHeader.biSize);
output((int)lpColors);
int dibCols = pow(2.0,pDIB->bmiHeader.biBitCount);
output(dibCols);
SetDIBColorTable(hMemDC,0,dibCols,lpColors);
output(dibCols);
void* bitArray = pDIB + pDIB->bmiHeader.biSize+dibCols *sizeof(RGBQUAD);
int sizeOfBitArray = dwSize - pDIB->bmiHeader.biSize+dibCols *sizeof(RGBQUAD);
 for(int i=0;i<dibCols;i++)
 {
    lpColors[i].rgbRed=0;
    lpColors[i].rgbBlue=0;
    lpColors[i].rgbGreen=0;
    lpColors[i].rgbReserved=0;
 } 
 output(SetDIBColorTable(hMemDC,0,dibCols,lpColors));
 int depth =bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;
 int nChannels = 3;
 IplImage* image= cvCreateImage(cvSize(width,height),depth,3);
 output(GetDIBits(hMemDC,b,0,height,bitArray,pDIB,DIB_RGB_COLORS));
 image->imageData = (char*)bitArray;
 cvSaveImage("c:\img.jpg",image);

我试图将 CBitmap 转换为 DIB,但它在控制台应用程序中不起作用我如何获得设备上下文?SetDIBColorTable 和 GetDIBits 都不起作用

4

0 回答 0