bool opfile(char* ofn)
{
BITMAPFILEHEADER bithead_file=*new BITMAPFILEHEADER;
BITMAPINFOHEADER bithead_info=*new BITMAPINFOHEADER;
BITMAPINFO  bit_info=*new BITMAPINFO;
RGBQUAD rgb=*new RGBQUAD;
LPBITMAPINFO* bitinfo; SECURITY_ATTRIBUTES *sa=new SECURITY_ATTRIBUTES;
DWORD headsize,size_file,size_info,size_rgb,size_data,*wd=new DWORD; headsize=(DWORD)sizeof(bithead_file); HANDLE hFile=new HANDLE; hFile= CreateFile(ofn, GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL); if (hFile==INVALID_HANDLE_VALUE)   
{
MessageBox(NULL,"e","e",MB_OK);
} SetFilePointer(hFile,0,0,FILE_BEGIN);

if(!ReadFile(hFile,&bithead_file,sizeof(BITMAPFILEHEADER),&size_file,NULL)) MessageBox(NULL,"error","e",MB_OK);
if(bithead_file.bfType!=0x4d42)
{
MessageBox(NULL,"not bmp","error",MB_OK);
return 0;
}
if(!ReadFile(hFile,&bithead_info,sizeof(BITMAPINFOHEADER),&size_info,NULL)) MessageBox(NULL,"error","e",MB_OK);
if(!ReadFile(hFile,&bit_info,sizeof(BITMAPINFO),&size_info,NULL)) MessageBox(NULL,"error","e",MB_OK);
//if(!ReadFile(hFile,&rgb,sizeof(RGBQUAD),&size_rgb,NULL)) MessageBox(NULL,"error","e",MB_OK); DWORD size=bithead_file.bfSize-sizeof(BITMAPFILEHEADER)-sizeof(BITMAPINFOHEADER); SetFilePointer(hFile,(long)bithead_file.bfOffBits,0,FILE_BEGIN); DWORD bitcount;
LONG bitwidth,bitheight,datasize;
bitcount=bithead_info.biBitCount;
bitwidth=bithead_info.biWidth;
bitheight=bithead_info.biHeight; datasize=bitheight*WIDTHBYTES(bithead_info.biBitCount); HANDLE temp=(HANDLE)::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,size);
LPSTR ptr=(LPSTR)temp; if(!ReadFile(hFile,ptr,datasize,&size_info,NULL)) MessageBox(NULL,"error","e",MB_OK); HBITMAP bitmap=*new HBITMAP;
HDC dc=GetDC(hWnd),MemDC=CreateCompatibleDC(dc); const BITMAPINFOHEADER* ptr_headinfo=&bithead_info;
const BITMAPINFO* ptr_info=&bit_info;
UINT uStartScan; HANDLE hdib=(HANDLE)GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,bithead_file.bfSize); HBITMAP hBit;
  //SetDIBitsToDevice(MemDC,0,0,bithead_info.biWidth,bithead_info.biHeight,0,0,uStartScan,bithead_info.biHeight,ptr,ptr_info,DIB_RGB_COLORS);
hBit=CreateDIBitmap(MemDC,ptr_headinfo,CBM_INIT,ptr,ptr_info,DIB_RGB_COLORS);
if(hBit=0) MessageBox(NULL,"CreateDIBitmap出错","e",MB_OK);
//SelectObject(MemDC,hBit);
BitBlt(dc,0,0,bithead_info.biWidth,bithead_info.biHeight,MemDC,0,0,SRCCOPY); delete(sa);
//delete(&bithead_file);
//delete(bithead_info); return 1;
}bithead_info,bit_info已经正确指向结构``最后bittle后为什么没有图片显示??

解决方案 »

  1.   

    CBitmap m_bitmap;m_bitmap.LoadBitmap(MapId);
    CDC dcImage;
    if (!dcImage.CreateCompatibleDC(&dc))
    return;

    BITMAP bm;
    m_bitmap.GetBitmap(&bm);

    CBitmap* pOldBitmap = dcImage.SelectObject(&m_bitmap);
    //dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
    dc.SetStretchBltMode(STRETCH_HALFTONE|STRETCH_ORSCANS);
    dc.StretchBlt(0,0,WinRect.right,WinRect.bottom - 80,&dcImage,0,0,
    bm.bmWidth, bm.bmHeight,SRCCOPY); dcImage.SelectObject(pOldBitmap);
    dcImage.DeleteDC();
      

  2.   

    //显示图片函数
    BOOL ShowPIC(CDC *pDC, CString strPath, CRect rect,int ShowType)
    {
    CPen newpen(PS_DOT,1,RGB(255,255,255));
    CPen *pOldPen=pDC->SelectObject(&newpen);
    pDC->Rectangle(rect);
    pDC->SelectObject(pOldPen);

    //显示BMP JPG GIF等格式的图片
        IStream *pStm;  
        CFileStatus fstatus;
        CFile file;  
        LONG cb;  

    HGLOBAL hGlobal;
        //打开文件并检测文件的有效性
        if (file.Open(strPath,CFile::modeRead)&&
            file.GetStatus(strPath,fstatus)&& 
            ((cb = fstatus.m_size) != -1)) 
        {  

            hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb); 
            LPVOID pvData = NULL;  
            if (hGlobal != NULL)  
            {  
                pvData = GlobalLock(hGlobal);
                if (pvData != NULL)  
                {  
                    file.ReadHuge(pvData, cb);  
                    GlobalUnlock(hGlobal);  
                    CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);  
                } 
            } 
        }
        else
        {
            return false;
        } 
    //打开文件结束
        

        //显示JPEG和GIF格式的图片,GIF只能显示一帧,还不能显示动画,
    //要显示动画GIF请使用ACTIVE控//件。

        IPicture *pPic; 
        //load image from file stream

        if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic))) 
        { 
            OLE_XSIZE_HIMETRIC hmWidth;  
            OLE_YSIZE_HIMETRIC hmHeight; 
            pPic->get_Width(&hmWidth);  
            pPic->get_Height(&hmHeight);
            double fX,fY;  
            //得到图片的高度与宽度
            fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/
    ((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);  
            fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/
    ((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);  

    long nwidth,nheight,nleft,ntop;
    int Rx,Ry;
    Rx=rect.Width();
    Ry=rect.Height();
    nheight=Ry;
    nwidth=Rx;
    nleft=rect.left;
    ntop=rect.top;
    //检测显示模式
    //是否拉伸充满区域显示
    if(ShowType==1)
    {
    nheight=Ry;
    nwidth=Rx;
    nleft=rect.left;
    ntop=rect.top;
    }
    //是否居中且按正常比例显示
    else if(ShowType==2)
    {
    if(fX<Rx) nwidth=(long)fX;
    if(fY<Ry)     nheight=(long)fY;

    if((fY>Ry)||(fX>Rx))
    {
    double temp,temp2;
    temp=Ry/fY;
    temp2=Rx/fX;
    if(temp<temp2)
    {
    nwidth=(long)(fX*temp);
    nleft=(Rx-nwidth)/2+rect.left;
    }
    else if(temp>=temp2)
    {
    nheight=(long)(fY*temp2);
    ntop=(Ry-nheight)/2+rect.top;
    }
    }
    }


            //用 Render函数显示图片
            //if(FAILED(pPic->Render(*pDC,rect.left,rect.top ,rect.Width(),rect.Height(),0,
    if(FAILED(pPic->Render(*pDC,nleft,ntop,nwidth,nheight,0,
    hmHeight,hmWidth,-hmHeight,NULL)))  
            {
                pPic->Release();
                return false;
            }
               pPic->Release();  
        }  

        else  
        {
            return false;  
        }
    //释放内存
        GlobalUnlock(hGlobal);  
    GlobalFree(hGlobal);      return true;}
      

  3.   

    希望别拿例子上的MFC过来`只希望达人能给我一些指点`,了解我的代码里到底有什么错误`~
      

  4.   

    HANDLE hFile=new HANDLE;        // 内存泄露!! 直接HANDLE hFile;即可
    HBITMAP bitmap=*new HBITMAP;   // 内存泄露!! 多余
    //SelectObject(MemDC,hBit);     // 必须选入!!
    ptr,ptr_info                    // 最好为整块内存里面的两个地址!!Bitblt 应该放在OnDraw() 或OnPaint() 事件内 
    否则一刷新你就看不见