WIN32:尝试显示图像时出现空白屏幕

时间:2015-01-23 23:55:01

标签: image winapi

我正在尝试使用WIN32 API在创建的简单窗口上显示图像。我使用常量值100填充每个字节。但窗口上没有显示任何内容。我正在使用16位显示器。以下是我的代码。任何帮助将不胜感激:

#include <stdio.h>

#include <windows.h>

LRESULT CALLBACK EventProcessor (HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{ return(DefWindowProc(hWnd,uMsg,wParam,lParam)); }

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
                   LPSTR     lpCmdLine,int nCmdShow)
{
WNDCLASS        wc;
HWND            WindowHandle;
int         ROWS = 512,COLS=512;
unsigned char   *disp;
BITMAPINFO      *bm_info;
HDC         hDC;
int i;

wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc=(WNDPROC)EventProcessor;
wc.cbClsExtra=wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=wc.lpszMenuName=NULL;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName="Image Window Class";
if (RegisterClass(&wc) == 0)
  exit(0);

WindowHandle=CreateWindow("Image Window Class","Demo",
                          WS_OVERLAPPEDWINDOW,
                          10,10,COLS,ROWS,
                          NULL,NULL,hInstance,NULL);
if (WindowHandle == NULL)
  {
  MessageBox(NULL,"No window","Try again",MB_OK | MB_APPLMODAL);
  exit(0);
  }
ShowWindow (WindowHandle, SW_SHOWNORMAL);

bm_info=(BITMAPINFO *)calloc(1,sizeof(BITMAPINFO) + 256*sizeof(RGBQUAD));
hDC=GetDC(WindowHandle);

/* ... set up bmiHeader field of bm_info ... */

for (i=0; i<256; i++)   /* colormap */
  {
  bm_info->bmiColors[i].rgbBlue=bm_info->bmiColors[i].rgbGreen=bm_info->bmiColors[i].rgbRed=i;
  bm_info->bmiColors[i].rgbReserved=0;
  } 
disp = (unsigned char*)calloc(1, ROWS*COLS*2);

for(i=0; i<ROWS*COLS*2;++i)
{
    disp[i] = 100;
}

SetDIBitsToDevice(hDC,0,0,COLS,ROWS,0,0,
              0, /* first scan line */
              COLS, /* number of scan lines */
              disp,bm_info,DIB_RGB_COLORS);


ReleaseDC(WindowHandle,hDC);

free(bm_info);
MessageBox(NULL,"Press OK to continue","",MB_OK | MB_APPLMODAL);
}

由于

1 个答案:

答案 0 :(得分:1)

您没有正确初始化BITMAPINFOHEADER。在致电SetDIBitsToDevice之前添加以下内容:

bm_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bm_info->bmiHeader.biPlanes = 1;
bm_info->bmiHeader.biBitCount = 16;
bm_info->bmiHeader.biWidth = COLS;
bm_info->bmiHeader.biHeight = ROWS;