小弟想用strechblt 画出数组color里的8位灰度图像数据,未遂……请教大虾……代码如下:void CShow_pic2Dlg::DrawImg(CDC *pDC, int x, int y)
{
int color[16][16]={
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128},
{256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128},
{256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128},
{256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128},
{256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128},
{256,256,256,256,256,256,256,256,256,256,256,256,256,256,256,256},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
int imgWidth=16,imgHeight=16;
char     __bif[sizeof(BITMAPINFOHEADER)   +   sizeof(RGBQUAD)   *   256]; 
BITMAPINFO&   bif   =   *(BITMAPINFO*)__bif; 
bif.bmiHeader.biSize   =   sizeof(BITMAPINFOHEADER); 
bif.bmiHeader.biWidth   =   imgWidth;   
bif.bmiHeader.biHeight   =   imgHeight;   
bif.bmiHeader.biPlanes   =   1;   
bif.bmiHeader.biBitCount   =   8; 
bif.bmiHeader.biCompression   =   BI_RGB;   
bif.bmiHeader.biSizeImage   =   0;   
bif.bmiHeader.biXPelsPerMeter   =   96;   
bif.bmiHeader.biYPelsPerMeter   =   96;   
bif.bmiHeader.biClrUsed   =   0;   
bif.bmiHeader.biClrImportant   =   0;   
for(int   i   =   0;   i   <   256;   ++i) 

int   b   =   i; 
bif.bmiColors[i].rgbBlue   =   b; 
bif.bmiColors[i].rgbGreen   =   b; 
bif.bmiColors[i].rgbRed   =   b; 
bif.bmiColors[i].rgbReserved     =   0; 
}  StretchDIBits(*pDC,   x,   y   +   imgHeight-1,   imgWidth,   -imgHeight,   
0,0,   imgWidth,   imgHeight,   color, 
(BITMAPINFO*)&bif,   DIB_RGB_COLORS ,   SRCCOPY); 
}void CShow_pic2Dlg::OnButton2() 
{
// HWND   hWnd   =   AfxGetMainWnd()-> m_hWnd;
// CDC* pdc; //CDC是一个类,用于封装hDc
// HDC hDC= ::GetDC(hWnd);
CPaintDC dc(this);
CDC *pDC=&dc;
DrawImg(pDC, 0 , 0);
}

解决方案 »

  1.   

    CPaintDC dc(this);
     CDC *pDC=&dc;
     DrawImg(pDC, 0 , 0);
    -->
    CDC* pDC = GetDC();
     DrawImg(pDC, 0 , 0);
    ReleaseDC(pDC);CPaintDC不能用在这里
    A CPaintDC object can only be used when responding to a WM_PAINT message, usually in your OnPaint message-handler member function. 
      

  2.   

    另外你绘图的操作最好是放在OnPaint函数中去做。
      

  3.   


    首先很感谢你的解答!
    按照你的指点,我已经把原来的代码改为
    CDC* pDC = GetDC();
     DrawImg(pDC, 0 , 0);
    ReleaseDC(pDC);
    并且图像也画出来了,如下:可是我想象中的图片应该是一条条色带,白、灰、黑、白、灰、黑、白、灰、黑……
    为什么会是这样呢……请教……