问题描述
我的内存中的图像具有以下字节布局
I have an image in memory with the following byte layout
blue, green, red, alpha (32 bits per pixel)
不使用Alpha.
我想使用GDI将其绘制到窗口上.稍后,我可能只想将其较小的一部分绘制到窗口上.但是,存储器中的位图总是固定在一定的宽度上.高度.
I want to draw it to a window using GDI. Later I may want to draw only a smaller part of it to the window. But the bitmap in memory is always fixed at a certain width & height.
如何完成位图绘制操作?
How can this bitmap drawing operation be done?
推荐答案
SetDIBitsToDevice
和/或 StretchDIBits
可用于将像素数据直接绘制到 HDC
像素数据的格式可以在 BITMAPINFOHEADER .如果您的颜色值顺序不正确,则必须将压缩率设置为BI_BITFIELDS而不是BI_RGB,并在内存中的BITMAPINFOHEADER之后附加3个DWORD作为颜色掩码.
SetDIBitsToDevice
and/or StretchDIBits
can be used to draw pixel data directly to a HDC
if the pixel data is in a format that can be specified in a BITMAPINFOHEADER. If your color values are not in the correct order you must set the compression to BI_BITFIELDS instead of BI_RGB and append 3 DWORDs as the color mask after BITMAPINFOHEADER in memory.
case WM_PAINT:
{
RECT rc;
GetClientRect(hWnd, &rc);
PAINTSTRUCT ps;
HDC hDC = wParam ? (HDC) wParam : BeginPaint(hWnd, &ps);
static const UINT32 pixeldata[] = { ARGB(255,255,0,0), ARGB(255,255,0,255), ARGB(255,255,255,0), ARGB(255,0,0,0) };
BYTE bitmapinfo[FIELD_OFFSET(BITMAPINFO,bmiColors) + (3 * sizeof(DWORD))];
BITMAPINFOHEADER &bih = *(BITMAPINFOHEADER*) bitmapinfo;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = 2, bih.biHeight = 2;
bih.biPlanes = 1, bih.biBitCount = 32;
bih.biCompression = BI_BITFIELDS, bih.biSizeImage = 0;
bih.biClrUsed = bih.biClrImportant = 0;
DWORD *pMasks = (DWORD*) (&bitmapinfo[bih.biSize]);
pMasks[0] = 0xff0000; // Red
pMasks[1] = 0x00ff00; // Green
pMasks[2] = 0x0000ff; // Blue
StretchDIBits(hDC, 0, 0, rc.right, rc.bottom, 0, 0, 2, 2, pixeldata, (BITMAPINFO*) &bih, DIB_RGB_COLORS, SRCCOPY);
return !(wParam || EndPaint(hWnd, &ps));
}
这篇关于如何使用GDI将RGB位图绘制到窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!