1. Load image using package of choice - GDI+, CxImage, etc
2. Create HBITMAP from image
3. Call GetDIBits on the HBITMAP
When inserting image data into PDFs, one must perform the same steps. Here's the code I use for this task: (Note: you also have to add code to initialize and shutdown GDI+)
HBITMAP mLoadImg(WCHAR *szFilename)
{
HBITMAP result=NULL;
Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(szFilename,false);
bitmap->GetHBITMAP(NULL, &result);
delete bitmap;
return result;
}
char unsigned *myGetDibBits24(HBITMAP hBmpSrc)
{
BITMAPINFO bi;
BITMAP bm;
BOOL bRes;
char unsigned *buf, *result;
long outIndex, inIndex;
long width, height, x, y;
HDC memDC;
HBITMAP oldBmp;
memDC = CreateCompatibleDC(NULL);
oldBmp = (HBITMAP)SelectObject(memDC, hBmpSrc);
GetObject(hBmpSrc, sizeof(bm), &bm);
width = bm.bmWidth;
height = bm.bmHeight;
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = width;
bi.bmiHeader.biHeight = -height;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = 0; bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;
buf = new unsigned char[width * 4 * height];
bRes = GetDIBits(memDC, hBmpSrc, 0, bm.bmHeight, buf, &bi, DIB_RGB_COLORS);
SelectObject(memDC, oldBmp);
if (!bRes)
{
delete(buf);
buf = NULL;
}
DeleteDC(memDC);
result = new unsigned char[width*height*3];
outIndex = 0;
inIndex = 0;
for (y=0; y<height;> {
inIndex = y * bm.bmWidthBytes;
for (x=0; x<width;> {
result[outIndex++] = buf[inIndex+2];
result[outIndex++] = buf[inIndex+1];
result[outIndex++] = buf[inIndex];
inIndex += 4;
}
}
delete(buf);
return result;
}