写了一个处理图像的程序,编译通过。运行时,老是出错。0xc0000005 access violation 。调试运行指向红色代码。BOOL Matrix(CDib *pDib,UINT i, UINT j)
{
BYTE * lpSrc;             //指向源图像的指针
LONG lWidth;              //图像的宽度和
LONG lHeight;             //高度  if (pDib->IsEmpty ())
{
  AfxMessageBox("请先读入图像");
  return FALSE;
}
lWidth=pDib->m_lpBMIH ->biWidth ;
lHeight=pDib->m_lpBMIH ->biHeight ;
LPBYTE lpDIBBits=pDib->m_lpImage ;   //DIB位图数据块地址 LONG SWidth, SHeight;
SWidth=lWidth/i;          //每个方格的宽度
SHeight=lHeight/j;        //每个方格的高度 int a=0;
int b=0;
for(int m=0;m<lHeight; m+=SHeight)
   for(int n=0;n<lWidth*3;n+=SWidth)
    {
        lpDIBBits=lpDIBBits+m*SHeight+n*SWidth;
       //扫描划分的小方格的像素
        for(int h=0;h<SHeight;h++)
           for(int w=0;w<SWidth*3;w+=3)
{
     lpSrc=(unsigned char *)lpDIBBits+lWidth*3*w+h;     //需改
     if((*lpSrc)==0)
       {
a=a+1;
        }
     else if(*lpSrc==255)
       {
b=b+1;
       }
}
//方格中0值的像素多,则置0;否则,置255
     if(a>b)
        {
 for(int h=0;h<SHeight;h++)
   for(int w=0;w<SWidth*3;w+=3)
{
    lpSrc=(unsigned char *)lpDIBBits+lWidth*3*w+h;   
    *lpSrc=0;
}
        }
     else
      {
for(int h=0;h<SHeight;h++)
   for(int w=0;w<SWidth*3;w+=3)
     {
       lpSrc=(unsigned char *)lpDIBBits+lWidth*3*w+h;   
       *lpSrc=255;
      }
       } }
return TRUE;
}void CImageView::OnMatrix()    //菜单响应函数
{
// TODO: Add your command handler code here
CImageDoc* pDoc;
pDoc=GetDocument();

Matrix(pDoc->m_pImage,100,100); pDoc->UpdateAllViews (NULL);}