发件人: wangxl [mailto:hamo2k@163.com]
发送时间: 2006年9月13日 23:09
收件人: ‘wangxianlei@cabrtech.com’
主题: Displaying a Bitmap from a BMP File
Displaying a Bitmap from a BMP File
Rating:
Ramakrishna Talla (view profile) April 3, 2003 |
Environment: MFC, Visual C++ 6.0, Windows 2000
(continued)
The following code fragment shows how to read an image from a BMP file and display it in your MFC application window. You could see several articles on the same focus; the one I present here is very simple, with just a few lines of code. The code given below has been tested with Visual C++ 6.0 on Win 2000.
Create a single document interface application; select CFormView as the base class for the application’s view base class. Click on the resource tab on the project explorer to navigate to the resource editor and drag a button to the dialog resource. Double-click the button to add a handler to the application’s view class, as shown below.
void AppView::OnButton1()
{
}
Step 1: Load the Image File
Call the following:
CString szFilename ("C:\Talla\yourimg.bmp");
HBITMAP hBmp = (HBITMAP)::LoadImage(
NULL,
szFilename,
IMAGE_BITMAP,
0,
0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION
);
Step 2: Create a Bitmap Object and Attach It to the Object
CBitmap bmp; bmp.Attach(hBmp);
Step 3: Create a Memory DC and Select the BMP to It
You also need to store the old BMP pointer:
CClientDC dc(this);
CDC bmDC;
bmDC.CreateCompatibleDC(&dc);
CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
Step 4: Get the BMP Height and Width
Obtain this from CBitmap’s GetBitmap function.
BITMAP bi; bmp.GetBitmap(&bi);
Step 5: Get the Block of Pixels from memoryDC to the Screen
Use CClientDC’s BitBlt function. Next, re-select the old BMP. The complete code is as follows:
void AppView::OnButton1() { CString szFilename("C:\Talla\yourimg.bmp"); HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,szFilename, IMAGE_BITMAP,0,0, LR_LOADFROMFILE|LR_CREATEDIBSECTION); CBitmap bmp; bmp.Attach(hBmp); CClientDC dc(this); CDC bmDC; bmDC.CreateCompatibleDC(&dc); CBitmap *pOldbmp = bmDC.SelectObject(&bmp); BITMAP bi; bmp.GetBitmap(&bi); dc.BitBlt(0,0,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY); bmDC.SelectObject(pOldbmp); }
Borland CaliberRM Integrates with Visual Studio to Bring Sophistication to Requirements Management
Get DB2 Express-C 9. Free to Develop, Deploy, Distribute. No limits–just data. Download Now!
Webcast: Linux on Multi-Core–WAS CE and the Open Stack Appliance
Learn how to navigate the key issues that affect developing and deploying mobile applications.
Developer.com Webcast: Defining Your Own Software Development Methodology.