cclhccc 2013-09-24 07:21 采纳率: 0%
浏览 2084

BMP在picture上显示的问题

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图像编程,急求

  • 写回答

1条回答

  • 不要心急 2013-09-24 08:03
    关注

    一眼没法,看出什么问题。

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler