C++ resolves instances for the BMP format

  • 2020-04-02 02:51:59
  • OfStack

In this paper, the example of C++ for BMP format analysis method, to share for your reference. Specific methods are as follows:

This code is prone to the following errors:
1. Forget on_wm_paint ()   I can't draw it on the interface
2. Write it correctly

BYTE* pBits = (BYTE*)lpBase + pbitmapFileHeader->bfOffBits; 

Written on the  

BYTE* pBits =  pbitmapFileHeader->bfOffBits;

This is mainly the use of the previous article (link: #).

. CPP source file is as follows:

#include "ReadBMP.h"  
#include "resource.h" 
#include <afxdlgs.h > 
 
CMyApp theApp; 
BOOL CMyApp::InitInstance() 

    m_pMainWnd = new CMainWindow; 
    m_pMainWnd->ShowWindow(m_nCmdShow); 
    return TRUE; //You must return TRUE, or you will not enter the message loop and the interface will simply exit & NBSP; < br / > } 
 
//CMainWindow 
BEGIN_MESSAGE_MAP(CMainWindow, CWnd) 
ON_WM_CREATE() 
ON_WM_PAINT() 
ON_COMMAND(IDC_OPEN, OnOpen) 
END_MESSAGE_MAP() 
//Constructor & NBSP; < br / > CMainWindow::CMainWindow() 

    LPCTSTR lpszClassName = ::AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW, ::LoadCursorA(NULL, IDC_ARROW), (HBRUSH)(COLOR_3DFACE+1), theApp.LoadIcon(IDI_MAIN)); 
    CreateEx(WS_EX_CLIENTEDGE, lpszClassName, "xxx", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL); 

//Destructor & NBSP; < br / > CMainWindow::~CMainWindow() 

 

//Message mapping function & NBSP; < br / > int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) 

    //OutputDebugString("oncreate"); 
    CClientDC dc(this); 
    m_hMemDC = ::CreateCompatibleDC(dc); 
    m_nWidth = 0; 
    m_nHeight = 0; 
    //Setup menu & menu; < br / >     HMENU hMenu = ::LoadMenuA(theApp.m_hInstance, (LPCSTR)IDR_MENU); 
    ::SetMenu(m_hWnd, hMenu); 
    return 0; 

 
void CMainWindow::OnNcDestroy( ) 

    delete this; 

void CMainWindow::OnDestroy() 

     

void CMainWindow::OnPaint() 

    CPaintDC dc(this); 
    ::BitBlt(dc, 0, 0, m_nWidth, m_nHeight, m_hMemDC, 0, 0, SRCCOPY); 

void CMainWindow::OnOpen() 

    CFileDialog dlg(TRUE); 
    if (IDOK != dlg.DoModal()) 
    { 
        return; 
    } 
    HANDLE  hFile = ::CreateFile(dlg.GetPathName(), GENERIC_READ , FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 
    if (INVALID_HANDLE_VALUE == hFile) 
    { 
        return; 
    } 
    HANDLE  hFileMap = ::CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); 
    if (NULL == hFileMap) 
    { 
        return; 
    } 
    LPVOID lpBase = ::MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0); 
    if (NULL == lpBase) 
    { 
        return; 
    } 
    //Take three pieces of information from scratch: 1. The starting position of the bfOffBits bitmap data in the file, 2. The width of the image m_nWidth, 3. < br / >     BITMAPFILEHEADER* pbitmapFileHeader; 
    BITMAPINFO* pbitmapInfo; 
    pbitmapFileHeader = (BITMAPFILEHEADER*)lpBase; 
    if (pbitmapFileHeader->bfType != MAKEWORD('B','M')) 
    { 
        MessageBox("not bmp"); 
        ::UnmapViewOfFile(lpBase); 
        ::CloseHandle(hFileMap); 
        ::CloseHandle(hFile); 
    } 
    //DWORD bfOffBits = pbitmapFileHeader->bfOffBits; 
    BYTE* pBits = (BYTE*)lpBase + pbitmapFileHeader->bfOffBits; 
    pbitmapInfo = (BITMAPINFO*)((BYTE*)lpBase + sizeof(BITMAPFILEHEADER)); 
    m_nWidth = pbitmapInfo->bmiHeader.biWidth; 
    m_nHeight = pbitmapInfo->bmiHeader.biHeight; 
    //Displays BMP files to memory device & NBSP; < br / >     //Client area dc&cake; < br / >     CClientDC dc(this); 
    //Create a dc-compatible bitmap & NBSP; < br / >     HBITMAP hBitmap = ::CreateCompatibleBitmap(dc, m_nWidth, m_nHeight); 
    if (hBitmap == 0) 
    { 
        return; 
    } 
    //Bitmap selected into memory dc&cake; < br / >     ::SelectObject(m_hMemDC, hBitmap); 
 
    //Image data is placed in the established DC & NBSP; < br / >     ::SetDIBitsToDevice(m_hMemDC, 0, 0, m_nWidth, m_nHeight, 0, 0, 0, m_nHeight, pBits, pbitmapInfo, DIB_RGB_COLORS); 
 
    ::InvalidateRect(m_hWnd, NULL, TRUE); 
    ::DeleteObject(hBitmap); 
 
    ::UnmapViewOfFile(lpBase); 
    ::CloseHandle(hFileMap); 
    ::CloseHandle(hFile); 
 
}

. H header file is as follows:

#include <afxwin.h>  
 
class CMyApp:public CWinApp 

public: 
    virtual BOOL InitInstance(); 
}; 
 
//CMainWindow 
class CMainWindow:public CWnd 

public: 
    CMainWindow(); 
    ~CMainWindow(); 
 
protected: 
    HDC m_hMemDC; //Client compatible memory DC handle & NBSP; & have spent < br / >     UINT m_nWidth; //Width of BMP & NBSP; < br / >     UINT m_nHeight; //The height of BMP & NBSP; < br / >      
    //Message mapping & NBSP; < br / >     afx_msg void OnNcDestroy( ); 
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); 
    afx_msg void OnDestroy( ); 
    afx_msg void OnOpen(); 
    afx_msg void OnPaint(); 
    DECLARE_MESSAGE_MAP() 
};

Hope that the article described in the C++ programming to help you.


Related articles: