Displaying a Bitmap from a BMP File

Environment: MFC, Visual C++ 6.0, Windows 2000

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);
}



Comments

  • Can not display bitmap on the screen !

    Posted by Tuyenkhuc on 03/10/2015 01:23am

    Thank bro, But when I debug this application, it displays nothing , that means I can not display the bitmap on the screen? First, I puted whole the code on OnPaint() function, it doesnt work,then I changed into OnDraw(), it doesnt work either. Any explain here, please help.

    Reply
  • Cheap Oakley Romeo 2.0 90% off sale

    Posted by yulxayygi on 06/26/2013 05:02am

    fOakley ,Oakley sunglasses lens sunglasses sale silver announcement can be a milestone in the glasses. This new make of sunglasses provides you with an unprecedented enjoyment and fitness. Oakley sunglasses are known to be very beneficial due to the streamlined framework and a selection of lens sports enthusiasts ideal choice. Cheap ray ban ,If you are searching to very much leave there, you need the look of this beautiful heroine, you'll need can be a pair of Oakley. In fact, you could be more eye-catching casing a terrific number of sunglasses, including an incredible beveled plastic frames, ebony frame of silver, old watches, in conjunction with your favorite color. fake Oakley Gascan ,Oakley is really a serious movement, a lively personality behind the target of sunglasses. Oakley complex technical and brutal character, on the selling point of sportsmen and ladies. You could wear glasses a bold fashion statement, for those who are actually increasingly common disturbed and located something else to be concerned about. Oakley's tone is a straightforward tool, the sun's light release, as well as the harmful ultraviolet light inside human eye, even if the sun's rays. Oakley is an entertainment brand, however the men and women could also have an affordable price, it's a great variety. The truth is, everyone from your Oakley sunglasses provide 100% Ultraviolet protection. Even non-colored fashion Oakley sunglasses give you the same Ultraviolet protection. Beyond the design of high-speed and quality of the expansion of high-protection framework. Designers and professional sunglasses, for example the employment of a various activities, more than the vast majority of expensive sunglasses. However, you'll discover various on-line fashion Oakley sunglasses available affordable price. Potential buyers will likely merely a maximum nearby the sample design concept with style, together with Oakley sunglasses, design and style, and the negligence of its coloring. Recently introduced the "wearable electronics" to recover weapons to produce Bluetooth solutions, and implantation with the solution framework to eliminate the necessitie on the headset, headphone, line and wire. In addition to Oakley sunglasses, into the tar within the clothing of gold, like glass bangles, natural expensive jewelry, equipment, bags along with other products. You can't squeeze price now so many different brands of quality also to fight for your attention, it is often challenging to determine what to decide on, which can make you more income.

    Reply
  • Thanks

    Posted by kamelee on 08/08/2012 06:25am

    I begin to study VC++, so some simple problems made me spent much time to think about the reason, this is the problem, thank you very much.

    Reply
  • Many Thanks!

    Posted by naree_oo on 05/27/2010 03:35am

    Many Thanks!

    Reply
  • It was very useful for me!

    Posted by gkpandi on 10/27/2009 06:25am

    Thanks a lot.....

    Reply
  • Displaying a Bitmap from a BMP file

    Posted by samadejacobs on 01/25/2006 08:23pm

    This works pretty fine Need to extract the bytestream from the Loaded bmp file I also need a source code to play and audio file and also extract its component frequency

    Reply
  • it works

    Posted by gaozhengwei on 11/17/2005 07:20am

    but also need some refinement.such as dealing with the WM_PAINT msg

    Reply
  • good

    Posted by parveenmittal on 11/16/2005 05:45am

    very well explained with simple steps. thanks Parveen

    Reply
  • Thank you

    Posted by sucer on 05/16/2004 11:52pm

    very useful for the beginners,like me,and do you have some materal in jpeg2k? I 'll be very appriciated if you could send it to me,if you have.

    Reply
  • Thanks a lot

    Posted by Legacy on 02/03/2004 12:00am

    Originally posted by: maxcode

    It is good example for me to develop my programm
    thanks!

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there's simply not enough quality talent to go around. In response, companies often resort to inferior solutions -- hiring substandard developers and engineers, recruiting talent on a part-time or temporary basis, poaching people from competitors, or burdening an already stressed IT staff for more of their labor. Fortunately, there's a better solution. Read this white paper to learn the business value of …

  • By providing developers with the right tools to detect, understand, and fix problems early, your business can simplify software development, shorten development lifecycles, and improve the quality of software code. The end result is increased innovation, secure applications, and a faster time to market — all at a lower cost.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date