我想读BMP文件,并显示。
我想在点OPEN后得到路径保存到m_str中,可是出错。
代码如下:
BOOL CSenbmpDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;

// TODO: Add your specialized creation code here
    m_str=lpszPathName;
return TRUE;
}
以下是OnDraw中
CDC   MemDC;   
  CString   str;
  str=pDoc->m_str;
  HBITMAP   hBitmap=(HBITMAP)LoadImage(NULL,str,   IMAGE_BITMAP,   0,   0,   LR_LOADFROMFILE|LR_CREATEDIBSECTION);   
  CBitmap   *pBitmap   =   new   CBitmap;   
    
  pBitmap->Attach(hBitmap);         
  //pBitmap   =   CBitmap::FromHandle(hBitmap);   
  //上述二句话实际效果完全相同.         
    
  MemDC.CreateCompatibleDC(pDC);         
  MemDC.SelectObject(pBitmap);   
  pDC->BitBlt(0,   0,800,   800,   &MemDC,   0,   0,   SRCCOPY);     
  pBitmap->Detach();   
  delete   pBitmap;
求高手指点,谢谢!

解决方案 »

  1.   

    str必须是资源
    外部的不能这样做可以用CImage或者其他的
      

  2.   

    lpszPathName是你打开的文档目录路径。又怎么会是你的BMP所在路径呢?就算BMP在你的程序目录下,也得加上BMP的文件名啊。你要取得是你的BMP的全路径名。
      

  3.   

    lpszPathName是你打开的文档目录路径。又怎么会是你的BMP所在路径呢?就算BMP在你的程序目录下,也得加上BMP的文件名啊。你要取得是你的BMP的全路径名。
      

  4.   

    lpszPathName是你打开的文档目录路径。又怎么会是你的BMP所在路径呢?就算BMP在你的程序目录下,也得加上BMP的文件名啊。你要取得是你的BMP的全路径名。
      

  5.   

    lpszPathName是你打开的文档目录路径。又怎么会是你的BMP所在路径呢?就算BMP在你的程序目录下,也得加上BMP的文件名啊。你要取得是你的BMP的全路径名。
      

  6.   

    lpszPathName是你打开的文档目录路径。又怎么会是你的BMP所在路径呢?就算BMP在你的程序目录下,也得加上BMP的文件名啊。你要取得是你的BMP的全路径名。
      

  7.   

    lpszPathName是你打开的文档目录路径。又怎么会是你的BMP所在路径呢?就算BMP在你的程序目录下,也得加上BMP的文件名啊。你要取得是你的BMP的全路径名。
      

  8.   

    m_str=lpszPathName;
    这里你可以代替为将位图文件导入内存(loadimage),然后,通过操作内存去显示。
      

  9.   

    BOOL   GetScreenCaps(int   *x,int   *y) //得到屏幕的分辨率 

    HDC   hScrDC; hScrDC   =   CreateDC("DISPLAY",   NULL,   NULL,   NULL); 
    *x   =   GetDeviceCaps(hScrDC,   HORZRES); 
    *y   =   GetDeviceCaps(hScrDC,   VERTRES); 
    DeleteObject(hScrDC); return   TRUE; 

    /*将鼠标光标画到指定的地方去*/ 
    BOOL   DrawMouse(HDC   hdc,HCURSOR   hcursor) 

    POINT   CursorPos; 
    ICONINFO   IconInfo; // hcursor   =   ::GetCursor(); 
    //   If   the   mouse   cursor   handle   is   invalid   then   forget   it 
    if   (hcursor   ==   NULL) 
    return   FALSE; //   Get   the   cursor   position 
    if   (!GetCursorPos(&CursorPos)) 
    return   FALSE; //   Translate   position   for   hotspot 
    if   (GetIconInfo(hcursor,   &IconInfo)) 

    CursorPos.x   -=   ((int)   IconInfo.xHotspot); 
    CursorPos.y   -=   ((int)   IconInfo.yHotspot); 
    if   (IconInfo.hbmMask   !=   NULL) 
    DeleteObject(IconInfo.hbmMask); 
    if   (IconInfo.hbmColor   !=   NULL) 
    DeleteObject(IconInfo.hbmColor); 
    } //   Draw   the   cursor 
    DrawIconEx( 
    hdc, //   handle   to   device   context   
    CursorPos.x,   CursorPos.y, // 
    hcursor, //   handle   to   icon   to   draw   
    0,0, //   width   of   the   icon   
    0, //   index   of   frame   in   animated   cursor   
    NULL, //   handle   to   background   brush   
    DI_NORMAL   ¦   DI_COMPAT //   icon-drawing   flags   
    ); return   TRUE; 

    //取屏幕数据,lpRect指定取屏幕的范围,len指定buf的大小 
    // 
    // 当buf不为NULL且长度足够拷贝数据时,返回图片数据大小 
    // 当buf为NULL或者长度不够拷贝数据时,返回图片数据大小 
    // 当执行失败时返回0 
    unsigned   GetScrnData(LPRECT   lpRect,   unsigned   char   *   buf,   unsigned   len) 

    HDC               hScrDC,   hMemDC;           //   屏幕和内存设备描 述表   
    HBITMAP         hBitmap,   hOldBitmap;       //   位图句柄 //   位图宽度和高度 
    int   iWidth,   iHeight; 
    iWidth   =   lpRect-> right-   lpRect-> left; 
    iHeight   =   lpRect-> bottom   -   lpRect-> top; //为屏幕创建设备描述表 
    hScrDC   =   CreateDC("DISPLAY",   NULL,   NULL,   NULL); //   获得屏幕分辨率 
    int               xScrn,   yScrn;                   
    xScrn   =   GetDeviceCaps(hScrDC,   HORZRES); 
    yScrn   =   GetDeviceCaps(hScrDC,   VERTRES); //为屏幕设备描述表创建兼容的内存设备描述表 
    hMemDC   =   CreateCompatibleDC(hScrDC); 
    hBitmap   =   CreateCompatibleBitmap(hScrDC,   iWidth,iHeight); 
    hOldBitmap   =   (HBITMAP)SelectObject(hMemDC,   hBitmap); 
    StretchBlt(hMemDC,   0,   0,   iWidth,   iHeight,   hScrDC,   0,   0,   xScrn,yScrn,   SRCCOPY); /*这里把鼠标的图标画到图像中去*/ 
    if(buf   &&   len) 

    // DrawMouse(hMemDC); 
    HCURSOR   cur   =   ::GetCursor(); 
    POINT   curPos; 
    GetCursorPos(&curPos); 
    DrawIcon(hMemDC,curPos.x,curPos.y,cur); 

    //   创建位图头 
    BITMAPINFO bmpInfo; 
    ZeroMemory(&bmpInfo,sizeof(BITMAPINFO)); 
    bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); 
    bmpInfo.bmiHeader.biWidth   =   iWidth; 
    bmpInfo.bmiHeader.biHeight   =   iHeight; 
    bmpInfo.bmiHeader.biPlanes   =   1; 
    bmpInfo.bmiHeader.biBitCount   =   24; 
    bmpInfo.bmiHeader.biCompression   =   BI_RGB; //图片信息 
    if   (0   ==   GetDIBits(hMemDC,   hBitmap,   0,   iHeight,   NULL,   &bmpInfo,   DIB_RGB_COLORS)) 

    MessageBox(NULL,"GetDIBits   Fail   !","ERROR",MB_OK); 
    return   0; 
    } //当接收缓冲为空或者,长度不够时返回需要的长度 
    if   (NULL   ==   buf   ¦ ¦   len   <   bmpInfo.bmiHeader.biSizeImage) 

    // return   bmpInfo.bmiHeader.biSizeImage; 
    goto   hah; 
    } //把图片数据拷至缓冲中去 
    GetDIBits(hMemDC,   hBitmap,   0,   iHeight,   buf,   &bmpInfo,   DIB_RGB_COLORS); hBitmap   =   (HBITMAP)SelectObject(hMemDC,   hOldBitmap); 
    hah: 
    int   erro   =   DeleteObject(hBitmap); 
    erro   =   DeleteDC(hScrDC); 
    erro   =   DeleteDC(hMemDC); 
    // DeleteObject(hBitmap); //   返回位图句柄 
    return   bmpInfo.bmiHeader.biSizeImage; 
    } void   DisplayPicture(HDC   hdc,char   *pdata,int   datalen,int   s_w,int   s_h,int   w,int   h) //显示图像 

    BITMAPINFO bmpInfo; 
    ZeroMemory(&bmpInfo,sizeof(BITMAPINFO)); 
    bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); 
    bmpInfo.bmiHeader.biWidth   =   0; 
    bmpInfo.bmiHeader.biHeight   =   0; 
    bmpInfo.bmiHeader.biPlanes   =   1; 
    bmpInfo.bmiHeader.biBitCount   =   24; 
    bmpInfo.bmiHeader.biCompression   =   BI_RGB; 
    bmpInfo.bmiHeader.biSizeImage   =   datalen; 
    bmpInfo.bmiHeader.biWidth   =   w; 
    bmpInfo.bmiHeader.biHeight   =   h; StretchDIBits(hdc,0,0,s_w,s_h, 
    0,0,w,h,pdata,&bmpInfo, 
    DIB_RGB_COLORS,SRCCOPY); 

    回lz  这下该够你用了把   以上代码   经过时间考验   保证能用
      

  10.   

    可以用LoadImage函数加载图像文件,你跟踪一下看在View::OnDraw中str的值
    CString       str; 
        str=pDoc-> m_str;  //在这里和下一行设一个断点
        HBITMAP       hBitmap=(HBITMAP)LoadImage(NULL,str,       IMAGE_BITMAP,       0,       0,       LR_LOADFROMFILE ¦LR_CREATEDIBSECTION);  还有一个方法是把str用一个固定路径名代替。如:D:\\test.bmp。还有一个方法是将你菜单的Open事件放到View中,用一个CString变量记录文件名,然后在View中读取它  
      

  11.   

    void CShowPicture1View::OnFileOpen()
    {
    // TODO: Add your command handler code here
    TCHAR strFilter[] = { TEXT("Picture Files (*.bmp)|*.bmp||") };
    CFileDialog dlg(TRUE, TEXT(".bmp"), NULL, 0, strFilter); if( dlg.DoModal() == IDOK )
    {
    strFilename = dlg.GetFileName();  //strFilename 是View中定义的一个成员变量
    Invalidate();
    }
    }
    void CShowPicture1View::OnDraw(CDC* pDC)
    {
    CShowPicture1Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
    return; // TODO: add draw code for native data here
    if( strFilename != "" )
    {
    BITMAP bmpProperties; // Create a bitmap handle using the name of the file
    HBITMAP bmpHandle = (HBITMAP)LoadImage(NULL,
                                   strFilename,
                   IMAGE_BITMAP,
                   0,
           0,
           LR_LOADFROMFILE); CBitmap bmpPicture;
    CDC mdcPicture; // Convert the Win32 bitmap handle into an MFC bitmap object
    CBitmap *bmpFromHandle = bmpPicture.FromHandle(bmpHandle);
    bmpPicture.Attach(bmpHandle); // Call the Win32 GetObject() function to get the properties
    // of the bitmap and store them in a BITMAP structure
    ::GetObject(bmpPicture,
        sizeof(bmpProperties),
        &bmpProperties); // Create a compatible device context
    mdcPicture.CreateCompatibleDC(pDC);
    // Select the bitmap into the device context
    CBitmap * bmpPrevious = 
    mdcPicture.SelectObject(bmpFromHandle); // Using the dimensions store in the BITMAP object,
    // display the picture
    pDC->BitBlt(0, 0,
        bmpProperties.bmWidth, bmpProperties.bmHeight,
        &mdcPicture, 0, 0, SRCCOPY); // Get the dimensions of the new picture
    SIZE sizeTotal;
    sizeTotal.cx = bmpProperties.bmWidth;
    sizeTotal.cy = bmpProperties.bmHeight;
    // Change the scrolling area/dimensions of the view
    SetScrollSizes(MM_TEXT, sizeTotal); // Restore the bitmap
    pDC->SelectObject(bmpPrevious);
    // Delete the Win32 bitmap handle
    DeleteObject(bmpHandle);
    }
    }
      

  12.   

    同意 iyranly 说的可以用LoadImage函数加载图像文件,你跟踪一下看在View::OnDraw中str的值 
    CString               str;   
            str=pDoc->   m_str;     //在这里和下一行设一个断点 
            HBITMAP               hBitmap=(HBITMAP)LoadImage(NULL,str,               IMAGE_BITMAP,               0,               0,               LR_LOADFROMFILE   ¦LR_CREATEDIBSECTION);     还有一个方法是把str用一个固定路径名代替。如:D:\\test.bmp。 估计是路径的问题LoadImage 参数中 str 要有文件名的 D:\\test.bmp。