win32 CreateDIBSection显示 BMP 文件

win32 CreateDIBSection

win7 64位
创建 一个 MFC 对话框程序:
添加一个按钮:
按钮的 点击事件处理函数内容如下:

代码主要演示 函数的用法

void CDIBSECTDlg::OnBnClickedBtnShowBmp()
{
    // TODO: 在此添加控件通知处理程序代码

     OPENFILENAME ofn ;
     static TCHAR        szFileName [MAX_PATH], szTitleName [MAX_PATH] ;
     static TCHAR        szFilter[] = TEXT ("Bitmap Files (*.BMP)\0*.bmp\0")
                                      TEXT ("All Files (*.*)\0*.*\0\0") ;

    TCHAR   tszTempPath[_MAX_PATH];

    HWND hwnd;
    HDC hdc ;


    hwnd = AfxGetMainWnd()->m_hWnd;

    hdc =::GetDC(hwnd);

    ZeroMemory(tszTempPath, MAX_PATH);

    GetCurrentDirectory(MAX_PATH, tszTempPath);

    ofn.lStructSize       = sizeof (OPENFILENAME) ;
    ofn.hwndOwner         = NULL ;
    ofn.hInstance         = NULL ;
    ofn.lpstrFilter       = szFilter ;
    ofn.lpstrCustomFilter = NULL ;
    ofn.nMaxCustFilter    = 0 ;
    ofn.nFilterIndex      = 0 ;
    ofn.lpstrFile         = szFileName ;
    ofn.nMaxFile          = MAX_PATH ;
    ofn.lpstrFileTitle    = szTitleName ;
    ofn.nMaxFileTitle     = MAX_PATH ;
    ofn.lpstrInitialDir   = tszTempPath ;
    ofn.lpstrTitle        = NULL ;
    ofn.Flags             = 0 ;
    ofn.nFileOffset       = 0 ;
    ofn.nFileExtension    = 0 ;
    ofn.lpstrDefExt       = TEXT ("bmp") ;
    ofn.lCustData         = 0 ;
    ofn.lpfnHook          = NULL ;
    ofn.lpTemplateName    = NULL ;


    BOOL bRet = GetOpenFileName (&ofn);

    if(!bRet){

        return ;

    }


    printf("szFileName = %S\r\n",szFileName);

     _tprintf(_T("%s\r\n"),szFileName);

     BITMAP              bitmap ;

     HDC                 hdcMem ;

     BITMAPFILEHEADER bmfh ;
     BITMAPINFO     * pbmi ;
     BYTE           * pBits ;
     BOOL             bSuccess ;
     DWORD            dwInfoSize, dwBytesRead ;
     HANDLE           hFile ;
     HBITMAP          hBitmap ;

          // Open the file: read access, prohibit write access

     hFile = CreateFile (szFileName, GENERIC_READ, FILE_SHARE_READ,
                         NULL, OPEN_EXISTING, 0, NULL) ;

     if (hFile == INVALID_HANDLE_VALUE)
          return  ;

          // Read in the BITMAPFILEHEADER

     bSuccess = ReadFile (hFile, &bmfh, sizeof (BITMAPFILEHEADER), 
                          &dwBytesRead, NULL) ;

     if (!bSuccess || (dwBytesRead != sizeof (BITMAPFILEHEADER))         
                   || (bmfh.bfType != * (WORD *) "BM"))
     {
          CloseHandle (hFile) ;
          return  ;
     }

          // Allocate memory for the BITMAPINFO structure & read it in

     dwInfoSize = bmfh.bfOffBits - sizeof (BITMAPFILEHEADER) ;

     pbmi = (BITMAPINFO *)malloc (dwInfoSize) ;

     bSuccess = ReadFile (hFile, pbmi, dwInfoSize, &dwBytesRead, NULL) ;

     if (!bSuccess || (dwBytesRead != dwInfoSize))
     {
          free (pbmi) ;
          CloseHandle (hFile) ;
          return  ;
     }

          // Create the DIB Section

     hBitmap = CreateDIBSection (NULL, pbmi, DIB_RGB_COLORS,(void **) &pBits, NULL, 0) ;

     if (hBitmap == NULL)
     {
          free (pbmi) ;
          CloseHandle (hFile) ;
          return  ;
     }

          // Read in the bitmap bits

     ReadFile (hFile, pBits, bmfh.bfSize - bmfh.bfOffBits, &dwBytesRead, NULL) ;

     free (pbmi) ;
     CloseHandle (hFile) ;

     //return hBitmap ;


    if (hBitmap)
    {
        GetObject (hBitmap, sizeof (BITMAP), &bitmap) ;

        hdcMem = CreateCompatibleDC (hdc) ;
        SelectObject (hdcMem, hBitmap) ;

        BitBlt (hdc,    0, 0, bitmap.bmWidth, bitmap.bmHeight, 
                hdcMem, 0, 0, SRCCOPY) ;

        DeleteDC (hdcMem) ;
    }

    if (hBitmap)
    {
        DeleteObject (hBitmap) ;
        hBitmap = NULL ;
    }


}

在 OnInitDialog 中添加如下代码:

    AllocConsole(); 
    SetConsoleTitle(_T("debug console")); 
    freopen("CONOUT$","w",stdout); 

    printf("Hello\r\n");

显示效果:
win32 CreateDIBSection显示 BMP 文件_第1张图片

源码工程如下:
https://download.csdn.net/download/wowocpp/10515454

遇到问题:

某些BMP文件不能显示,源码包中的 DIBSECT\DIBSECT\Block2.BMP 这个图片就显示不了。

但是 同样一个图片
在如下帖子
win32 CreateDIBitmap 显示 BMP 文件
可以显示:
https://blog.csdn.net/wowocpp/article/details/80884251

win32 CreateDIBSection显示 BMP 文件_第2张图片

win32 CreateDIBSection显示 BMP 文件_第3张图片

查看 图片的 头文件信息:

Hello
szFileName = E:\VC\work\BMP_HEAD_TEST\BMP_HEAD_TEST\BMP_HEAD_TEST\Block2.BMP
E:\VC\work\BMP_HEAD_TEST\BMP_HEAD_TEST\BMP_HEAD_TEST\Block2.BMP
File size = 12344 bytes

BITMAPFILEHEADER
        .bfType = 0x4D42
        .bfSize = 12344
        .bfReserved1 = 0
        .bfReserved2 = 0
        .bfOffBits = 54

InfoHeadSize = 40
BITMAPINFOHEADER
BITMAPINFOHEADER
        .biSize = 40
        .biWidth = 64
        .biHeight = 64
        .biPlanes = 1
        .biBitCount = 24
        .biCompression = BI_RGB
        .biSizeImage = 0
        .biXPelsPerMeter = 15749
        .biYPelsPerMeter = 15749
        .biClrUsed = 0
        .biClrImportant = 0

szFileName = E:\VC\work\BMP_HEAD_TEST\BMP_HEAD_TEST\BMP_HEAD_TEST\waterfall.bmp
E:\VC\work\BMP_HEAD_TEST\BMP_HEAD_TEST\BMP_HEAD_TEST\waterfall.bmp
File size = 156296 bytes

BITMAPFILEHEADER
        .bfType = 0x4D42
        .bfSize = 156296
        .bfReserved1 = 0
        .bfReserved2 = 0
        .bfOffBits = 54

InfoHeadSize = 40
BITMAPINFOHEADER
BITMAPINFOHEADER
        .biSize = 40
        .biWidth = 280
        .biHeight = 186
        .biPlanes = 1
        .biBitCount = 24
        .biCompression = BI_RGB
        .biSizeImage = 156242
        .biXPelsPerMeter = 2834
        .biYPelsPerMeter = 2834
        .biClrUsed = 0
        .biClrImportant = 0


你可能感兴趣的:(win32-api)