我建立一个SDI工程,BMP图像是通过对话框打开,选择读入的,读入数据之后是这样的代码:
void CBMPTryView::OnViewOpen() 
{
CFileDialog fd(TRUE,"","",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"位图文件|*.bmp||",this);
    CString strFileName;
if(fd.DoModal() == IDOK)
{
strFileName = fd.GetPathName();
        CFile file;//读取位图信息
        if(!file.Open(strFileName,CFile::modeRead))
{
            return;
}
 //读取位图文件头
        if(file.Read(&BitmapHead,sizeof(BITMAPFILEHEADER))!=sizeof(BITMAPFILEHEADER))
{
           MessageBox("Fail to read the bmp!");
           return;
}
        if(BitmapHead.bfType!=0x4d42)
{
           MessageBox("sorry,the file is not Bitmap!");
            return;
}

        if(file.Read(&BitmapInfo,sizeof(BITMAPINFOHEADER))!=sizeof(BITMAPINFOHEADER))
{
            MessageBox("Fail the bmp file!");
            return;
}
        if(BitmapInfo.biBitCount!=24)
{
            MessageBox("sorry,current pro support 24 bits file!");
            return;
}
m_width = BitmapInfo.biWidth;
m_height = BitmapInfo.biHeight;
        bitmapinfo=(BITMAPINFO*)new char[sizeof(BITMAPINFOHEADER)];
        if(!bitmapinfo)
{
            MessageBox("Fail allocation memory !");
            return;
}
 /*把BMP位图信息头中的数据读取到位图信息结构中去.*/
        memcpy(bitmapinfo,&BitmapInfo,sizeof(BITMAPINFOHEADER));
 /*用来得到位图文件的大小*/
        DWORD dataByte=BitmapHead.bfSize-BitmapHead.bfOffBits;
        BmpData=(BYTE*)new char[dataByte];
        if(!BmpData)
{
            MessageBox("内存分配失败!");
            delete bitmapinfo;
            delete BmpData;
            return;
}
        if(file.Read(BmpData,dataByte)!=dataByte)
{
            MessageBox("读取位图数据失败!");
            return;
}
file.Close();
}

}
在View中怎么显示呢?肯定是在OnDraw()函数中显示,能给我实现位图读入显示的代码吗?谢谢各位

解决方案 »

  1.   

    在View中怎么显示呢?肯定是在OnDraw()函数中显示,能给我实现位图读入显示的代码吗?谢谢各位
    ===========
    显示一个BMP文件而已,用LoadImage加载,用Bitblt显示不就行了吗
      

  2.   

    给段代码给你:
    void CMy123View::OnDraw(CDC* pDC)
    {
    CMy123Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
    return;
    CDC mDC;

    HBITMAP hBitmap=(HBITMAP)::LoadImage(AfxGetApp()->m_hInstance,_T("C:\\1.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    BITMAP bt;
    GetObject(hBitmap,sizeof(bt),&bt);
    mDC.CreateCompatibleDC(pDC);
    mDC.SelectObject(hBitmap); pDC->BitBlt(0,0,bt.bmWidth,bt.bmHeight,&mDC,0,0,SRCCOPY);
    // TODO: 在此处为本机数据添加绘制代码
    }
      

  3.   

    我如果将图象数据读到一个Byte * 的变量里,请问如何显示?
      

  4.   

    用这个函数
    int StretchDIBits(
      HDC hdc,                      // handle to DC
      int XDest,                    // x-coord of destination upper-left corner
      int YDest,                    // y-coord of destination upper-left corner
      int nDestWidth,               // width of destination rectangle
      int nDestHeight,              // height of destination rectangle
      int XSrc,                     // x-coord of source upper-left corner
      int YSrc,                     // y-coord of source upper-left corner
      int nSrcWidth,                // width of source rectangle
      int nSrcHeight,               // height of source rectangle
      CONST VOID *lpBits,           // bitmap bits
      CONST BITMAPINFO *lpBitsInfo, // bitmap data
      UINT iUsage,                  // usage options
      DWORD dwRop                   // raster operation code
    );
      

  5.   

    这里有个例子
    http://faq.csdn.net/read/215527.html
      

  6.   

    一般的方法是用SetDIBits将Byte *中的数据写到一个HBITMAP中去。