简体   繁体   中英

WIN32: Getting a blank screen when trying to display an image

I am trying to use the WIN32 API for displaying an image on a simple window created. I am filling every byte with a constant value of 100. But nothing is getting displayed on the window. I am working on a 16-bit display. The below is my code. Any help would be much appreciated:

#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);
}

Thanks

You're not initialising the BITMAPINFOHEADER correctly. Add the following before the call to 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM