在没有 "visible" DC 的情况下在 HBITMAP 上绘制文本?

DrawText on HBITMAP without "visible" DC?

我需要在网络摄像头框架上打印时间戳并尝试下一步:

CameraFrameBufferSize = WebCam->GetFrameSize();
CameraFrameBuffer = (unsigned char *)realloc(CameraFrameBuffer, CameraFrameBufferSize);
unsigned char * buf = WebCam->CaptureFrame(); // returns pointer to RGB buffer of frame

HDC hDC = CreateCompatibleDC(NULL);
HFONT font = CreateFont(20, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, RUSSIAN_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH, "times");
RECT rect;
rect.left = 0;
rect.right = WebCam->GetFrameWidth();
rect.top = 10;
rect.bottom = 50;

HBITMAP hBitmap = CreateHBITMAPfromByteArray(hDC, WebCam->GetFrameWidth(), WebCam->GetFrameHeight(), 3, buf);

SelectObject(hDC, hBitmap);
SelectObject(hDC, font);
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(255,255,255));
string Text = GetTime("%Y.%m.%d %H:%M:%S");
DrawTextA(hDC, Text.c_str(), Text.size(), &rect, DT_CENTER | DT_WORDBREAK);

jpge::compress_image_to_jpeg_file_in_memory(CameraFrameBuffer, CameraFrameBufferSize, WebCam->GetFrameWidth() ,WebCam->GetFrameHeight(), 3, buf, CameraCompressor);

CameraFrame = string(reinterpret_cast<char*>(CameraFrameBuffer), CameraFrameBufferSize);
ReleaseDC(NULL, hDC);
DeleteObject(hBitmap);
DeleteObject(font);

CreateHBITMAPfromByteArray 是:

HBITMAP CreateHBITMAPfromByteArray(HDC hdc, int Width, int Height, int Colors, unsigned char* pImageData){
    LPBITMAPINFO lpbi = new BITMAPINFO;
    lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    lpbi->bmiHeader.biWidth = Width;
    lpbi->bmiHeader.biHeight = -Height;
    lpbi->bmiHeader.biPlanes = 1;
    lpbi->bmiHeader.biBitCount = Colors*8;
    lpbi->bmiHeader.biCompression = BI_RGB;
    lpbi->bmiHeader.biSizeImage = 0;
    lpbi->bmiHeader.biXPelsPerMeter = 0;
    lpbi->bmiHeader.biYPelsPerMeter = 0;
    lpbi->bmiHeader.biClrUsed = 0;
    lpbi->bmiHeader.biClrImportant = 0;

    return CreateDIBSection(hdc, lpbi, DIB_RGB_COLORS,(void **)&pImageData,NULL,0 );
}

保存测试图像文件(字符串 CameraFrame)时,我得到的只是一个没有文本的框架...

相机在后台拍摄,屏幕上没有任何显示,所以我不确定我选择的是 HDC。

一般来说,我有一个图像的 RGB 缓冲区,其中必须放置具有透明度的文本。如何实现?

看起来您正在压缩(和保存)原始相机帧缓冲区 buf,而不是位图缓冲区。 CreateDIBSection 的用法也是错误的,因为参数 #4 是一个输出参数,它应该接收一个指向 DIB 位值位置的指针,而你正试图将一个指针传递给那里现有的图像数据。