C + + for BMP format parsing instance _c language

Source: Internet
Author: User

This article describes the C + + for BMP format analysis method, share for everyone to reference. The specific methods are as follows:

When you write this code, you can easily get the following error:
1. Forget On_wm_paint () has been in the interface is not drawing out
2. Correct wording

Copy Code code as follows:
byte* pbits = (byte*) lpbase + pbitmapfileheader->bfoffbits;

Written a

Copy Code code as follows:
byte* pbits = pbitmapfileheader->bfoffbits;

This is mainly used in the previous article in the CWnd Framework.

The. CPP source files are as follows:

Copy Code code 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; Must return true, otherwise the message loop will not enter and the interface exits directly
}

CMainWindow
Begin_message_map (CMainWindow, CWnd)
On_wm_create ()
On_wm_paint ()
On_command (Idc_open, OnOpen)
End_message_map ()
Constructors
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
Cmainwindow::~cmainwindow ()
{

}
Message mapping functions
int cmainwindow::oncreate (lpcreatestruct lpcreatestruct)
{
OutputDebugString ("OnCreate");
CCLIENTDC DC (this);
M_HMEMDC =:: CreateCompatibleDC (DC);
m_nwidth = 0;
m_nheight = 0;
Set Menu
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
}
Three information from scratch structure: 1.bfOffBits bitmap data in the file starting position, 2.m_nwidth image width, 3.m_nheight image high
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;
Displaying BMP files to memory devices
Client Area DC
CCLIENTDC DC (this);
Create a bitmap compatible with the client area DC
Hbitmap hbitmap =:: CreateCompatibleBitmap (DC, M_nwidth, m_nheight);
if (hbitmap = 0)
{
Return
}
Bitmap selected into memory DC
:: SelectObject (M_HMEMDC, HBITMAP);

The image data is placed in the established DC
:: SetDIBitsToDevice (M_HMEMDC, 0, 0, m_nwidth, m_nheight, 0, 0, 0, m_nheight, pbits, Pbitmapinfo, dib_rgb_colors);

:: InvalidateRect (M_hwnd, NULL, TRUE);
::D eleteobject (HBITMAP);

:: UnmapViewOfFile (Lpbase);
:: CloseHandle (Hfilemap);
:: CloseHandle (hfile);

}

. h header documents are as follows:

Copy Code code 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; Memory DC handle compatible with client area
UINT M_nwidth; The width of BMP
UINT M_nheight; The height of BMP

Message map
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 ()
};

I hope this article will help you with the C + + program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.