void ShowMap::OnButton1()
{
// TODO: Add your control notification handler code here
/*m_ImageExist=FALSE;
m_width=m_heigh=0;
m_lpBuf=m_lpImage=NULL;*/
short m_bytecount=1;
CFileDialog dlg(TRUE, //BOOL bOpenFileDialog
"BMP", //LPCTSTR lpszDefExt = NULL
NULL, //LPCTSTR lpszFileName = NULL
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, //DWORD dwFlags
"BMP图象文件 (*.BMP) | *.BMP||",
NULL //CWnd* pParentWnd = NULL
);
if((dlg.DoModal()==IDOK))
{
CString filename=dlg.GetPathName();
///////////////////////////////////////
LoadImage(filename,&m_bytecount);
CDC *pDC = GetDlgItem(IDC_STATIC)->GetDC();
//CRect rc;
//GetDlgItem(IDC_STATIC)->GetClient(rc);
//CRect rc;
//CWnd *pWnd = GetDlgItem(IDC_STATIC);//参数为控件ID
//pWnd->GetClientRect(&rc);//rc为控件的大小
ShowImage(0,0,pDC);
}
}
short ShowMap::LoadImage(CString filename,short *bytecount)
{
BITMAPFILEHEADER bmpfh;
BITMAPINFOHEADER bmpih;
CFile cImagefile;
CFileException e;
// short tt;
if(!cImagefile.Open(filename,CFile::modeRead|CFile::typeBinary,&e) )
{
return -1;
#ifdef _DEBUG
afxDump << "File could not be opened " << e.m_cause << "\n";
#endif
}
cImagefile.Seek(0L,CFile::begin);
if(cImagefile.Read(&bmpfh,sizeof(BITMAPFILEHEADER))!=sizeof(BITMAPFILEHEADER))
{
cImagefile.Close();
return -1;
}
if(cImagefile.Read(&bmpih,sizeof(BITMAPINFOHEADER))!=sizeof(BITMAPINFOHEADER))
{
cImagefile.Close();
return -1;
}
*bytecount=bmpih.biBitCount/8;
m_Iwid=m_width=bmpih.biWidth;
m_Ihei=m_heigh=bmpih.biHeight;
long filesize=m_width*m_heigh* *bytecount;
if(m_ImageExist)
{
delete m_lpImage;
delete m_lpBuf;
m_ImageExist=FALSE;
}
m_lpBuf=new unsigned char[filesize];
if(!m_lpBuf)
{
AfxMessageBox("alloc buf fail",MB_OK);
return -1;
}
m_lpImage=new unsigned char[filesize];
if(!m_lpImage)
{
AfxMessageBox("alloc image buf fail",MB_OK);
delete m_lpBuf;
return -1;
}
BeginWaitCursor();
cImagefile.Seek(bmpfh.bfOffBits,CFile::begin);
long row_width;
row_width=m_width* *bytecount;
while ((row_width & 3) != 0) row_width++;
long i;
for(i=0;i<m_heigh;i++)
{
cImagefile.Read(&m_lpImage[(m_heigh-1-i)*m_width * *bytecount],m_width* *bytecount);
cImagefile.Seek(row_width-m_width* *bytecount,CFile::current);
}
cImagefile.Close();
EndWaitCursor();
for(i=0;i<filesize;i++)
m_lpBuf[i]=m_lpImage[i];
m_ImageExist=TRUE;
//SetTitle(filename);
return 0;
}
void ShowMap::ShowImage(int x,int y,CDC *pDC)
{
BITMAPINFO bmi_lsj;
short new_width=(m_width+3)/4*4;
char * lpBuf_local=new char[3L*new_width*m_heigh];
if(!lpBuf_local)
{
AfxMessageBox("malloc error",NULL,MB_OK);
return;
}
long i,j;
for(i=0;i<m_heigh;i++)
for(j=0;j<m_width;j++)
if(m_bytecount==1)
{
lpBuf_local[(i*(long)new_width+j)*3+0]=
lpBuf_local[(i*(long)new_width+j)*3+1]=
lpBuf_local[(i*(long)new_width+j)*3+2]=m_lpBuf[(m_heigh-1-i)*(long)m_width+j];
}
else
if(m_bytecount==3)
{
lpBuf_local[(i*(long)new_width+j)*3+0]=m_lpBuf[(m_heigh-1-i)*(long)m_width* 3+j*3+0];
lpBuf_local[(i*(long)new_width+j)*3+1]=m_lpBuf[(m_heigh-1-i)*(long)m_width* 3+j*3+1];
lpBuf_local[(i*(long)new_width+j)*3+2]=m_lpBuf[(m_heigh-1-i)*(long)m_width* 3+j*3+2];
}
memset(&bmi_lsj,0,sizeof(BITMAPINFO));
bmi_lsj.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
bmi_lsj.bmiHeader.biWidth=new_width;
bmi_lsj.bmiHeader.biHeight=m_heigh;
bmi_lsj.bmiHeader.biPlanes=1;
bmi_lsj.bmiHeader.biBitCount=24;
bmi_lsj.bmiHeader.biCompression=BI_RGB;
i=SetDIBitsToDevice(pDC->GetSafeHdc(),x,y,(WORD)bmi_lsj.bmiHeader.biWidth,
(WORD)bmi_lsj.bmiHeader.biHeight,0,0,0,(WORD)bmi_lsj.bmiHeader.biHeight,
lpBuf_local,( &bmi_lsj),DIB_RGB_COLORS);
delete(lpBuf_local);
}
我想知道我这个为什么无法正常显示呢,还缺了什么,菜鸟刚刚学MFC图像编程,急求