用VC的File::Open打开的文件怎么显示出来呀?现在要输出一幅位图,读出来的东西怎么转到DC中去?还有怎么保存成位图

解决方案 »

  1.   

    一句话说不好,你的邮箱告诉我,我给你一个例子多好亚,你可以把位图读入内存,然后在OnDraw()函数里面画出来,对于保存文件问题,我可以给你一个例子,你可以根据函数将相应的改动即可
    BOOL SaveBMP(HBITMAP hBitmap,const char *pFileName)
    {
    if (hBitmap == NULL)
    return FALSE;
    //验证pFileName
    if (pFileName == NULL)
    return FALSE; //创建DIB
    BITMAP bm;
    if (::GetObject(hBitmap,sizeof(BITMAP),&bm) <= 0)
    return FALSE;
    long nBmpWidth = bm.bmWidth;
    long nBmpHeight = bm.bmHeight;
    long nBmpWidthBytes = (nBmpWidth*3+3)/4*4;
    long nImageSize24 = nBmpWidthBytes*nBmpHeight;
    BYTE *pSrc = (BYTE *)::GlobalAlloc(GPTR,nImageSize24);//source dib
    if (pSrc == NULL)
    return FALSE;
    /////////////////////////////////////////////
    BITMAPINFOHEADER bifh;
    bifh.biSize=sizeof(BITMAPINFOHEADER);
    bifh.biWidth=nBmpWidth;
    bifh.biHeight=nBmpHeight;
    bifh.biPlanes=1;
    bifh.biBitCount=24;//24 bit
    bifh.biCompression=0;
    bifh.biSizeImage=nImageSize24;
    bifh.biXPelsPerMeter=0;
    bifh.biYPelsPerMeter=0;
    bifh.biClrUsed=0;
    bifh.biClrImportant=0;
    HDC hDC = ::GetDC(NULL);
    long nRet;
    nRet = ::GetDIBits(hDC,hBitmap,0,nBmpHeight,pSrc,(BITMAPINFO*)&bifh,DIB_RGB_COLORS);
    if (nRet != nBmpHeight)
    {
    DeleteDC(hDC);
    GlobalFree(pSrc);
    return FALSE;
    }
    ::ReleaseDC(NULL,hDC);
    //////////////////////////////////////////
    BITMAPFILEHEADER bfh;
    bfh.bfReserved1=bfh.bfReserved2=0;
    bfh.bfType=((WORD)('M'<< 8)|'B');
    bfh.bfSize=54+nImageSize24;
    bfh.bfOffBits=54;
    FILE *stream;
    if( (stream = fopen( pFileName, "w" )) != NULL )
    {
    fwrite(&bfh,1,sizeof(BITMAPFILEHEADER),stream);
    fwrite(&bifh,1,sizeof(BITMAPINFOHEADER),stream);
    fwrite(pSrc,1,nImageSize24,stream);
    fclose( stream );
    }
    else
    return FALSE;
    GlobalFree(pSrc);
    return TRUE;
    }