GDI GradientFill not working on offscreen bitmap
I am trying to use the GDI GradientFill function to draw on a offscreen bitmap, then BitBlt that to the screen.
But I always get a black bitmap... if I GradientFill directly to the screen it works.
Below is a sample app to see what I mean.
#pragma comment(lib, "msimg32.lib")
#include <windows.h>
const CHAR c_szWndClass[] = "GradientTestWnd";
const CHAR c_szWndTitle[] = "GradientTest";
const int c_nWndWidth = 1024;
const int c_nWndHeight = 768;
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASSEX wcx;
ZeroMemory(&wcx, sizeof(wcx));
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcx.lpfnWndProc = DefWindowProc;
wcx.hInstance = hInstance;
wcx.lpszClassName = c_szWndClass;
RegisterClassEx(&wcx);
HWND hwndMain = CreateWindowEx(
0,
c_szWndClass,
c_szWndTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
c_nWndWidth,
c_nWndHeight,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwndMain, SW_SHOW);
HDC hdc;
hdc = GetDC(hwndMain);
HDC hdcOffscreen = CreateCompatibleDC(hdc);
HBITMAP bitmap = CreateCompatibleBitmap(hdcOffscreen, c_nWndWidth, c_nWndHeight);
HBITMAP开发者_JAVA百科 old_bitmap = (HBITMAP) SelectObject(hdcOffscreen, bitmap);
TRIVERTEX vertices[2];
ZeroMemory(&vertices, sizeof(vertices));
vertices[0].Red = 0xFF00;
vertices[0].Green = 0x0000;
vertices[0].Blue = 0x0000;
vertices[0].x = 0;
vertices[0].y = 0;
vertices[1].Red = 0x0000;
vertices[1].Green = 0x0000;
vertices[1].Blue = 0xFF00;
vertices[1].x = c_nWndWidth;
vertices[1].y = c_nWndHeight;
GRADIENT_RECT rects[1];
ZeroMemory(&rects, sizeof(rects));
rects[0].UpperLeft = 0;
rects[0].LowerRight = 1;
// This works
//GradientFill(hdc, vertices, 2, rects, 1, GRADIENT_FILL_RECT_V);
// This doesn't
GradientFill(hdcOffscreen, vertices, 2, rects, 1, GRADIENT_FILL_RECT_V);
BitBlt(hdc, 0, 0, c_nWndWidth, c_nWndHeight, hdcOffscreen, 0, 0, SRCCOPY);
Sleep(5000);
SelectObject(hdcOffscreen, old_bitmap);
DeleteObject(bitmap);
DeleteDC(hdcOffscreen);
return 0;
}
the problem here is actually due to the initial state of the device context from which you are creating the compatible bitmap - in this line:
HBITMAP bitmap = CreateCompatibleBitmap(hdcOffscreen, c_nWndWidth, c_nWndHeight);
hdcOffscreen should instead be hdc - this is because the device context created here:
HDC hdcOffscreen = CreateCompatibleDC(hdc);
has by default a 1x1 monochrome bitmap selected into it - when you attempt to create a compatible bitmap from that, you will get a monochrome bitmap also. so if you do this instead:
HBITMAP bitmap = CreateCompatibleBitmap(hdc, c_nWndWidth, c_nWndHeight);
you should see your gradient :) old question that appears to be answered but i thought id just help clarify on exactly why its not working!
details/links:
http://msdn.microsoft.com/en-us/library/dd183489%28VS.85%29.aspx
When the memory DC is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high
http://msdn.microsoft.com/en-us/library/dd183488%28v=VS.85%29.aspx
The color format of the bitmap created by the CreateCompatibleBitmap function matches the color format of the device identified by the hdc parameter
hth :)
Create a DIB instead of a compatible bitmap.
Replace
HBITMAP bitmap = CreateCompatibleBitmap(hdcOffscreen, c_nWndWidth, c_nWndHeight);
with
BITMAPINFO BitmapInfo;
memset(&BitmapInfo, 0, sizeof(BITMAPINFOHEADER));
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biWidth = c_nWndWidth;
BitmapInfo.bmiHeader.biHeight = c_nWndHeight;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 32;
BitmapInfo.bmiHeader.biCompression = BI_RGB;
HBITMAP bitmap = CreateDIBSection(hdcOffscreen, &BitmapInfo, DIB_RGB_COLORS, NULL, NULL, 0);
精彩评论