Simple BMP Viewer

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Environment: Developed with VC 5.

This is a sample simple BMP viewer. It only shows the minimum code for loading a BMP from a file and showing it in a window. It does not do everything; I get frustrated trying to figure out how to do something simple using a sample that does everything. In particular, it does not do palettes, which is something that seems to be important, but also seems to be relatively complicated. I am not a graphics expert so I invite an expert to create another article documenting correspondingly simple palette processing and/or printing and print preview processing.

Consistent with the theme of keeping things simple, I will simply describe how to create a sample program and supply relevant code here. You can use this code as a sample for use in your project or use this code and instructions to create a complete working sample.

To create a complete working sample, start by generating an MDI application with a CScrollView view. You might want to de-select the option for printing and print preview. Then:

  1. Use ClassWizard to add processing for ID_FILE_NEW in your application class, but do nothing more with it, since we do not support bitmap creation.

  2. Use ClassWizard to add processing for DeleteContents in your document class and add the following to it:

    if (m_Bitmap.m_hObject != NULL)
  3. Use ClassWizard to add processing for OnOpenDocument in your document class and replace all the generated code with the following:

    if (IsModified())
    	TRACE0("Warning: OnOpenDocument replaces an unsaved document\n");
    HBITMAP hImage = (HBITMAP)LoadImage(NULL, lpszPathName, IMAGE_BITMAP,
    if (!hImage) {
    	DWORD LastError = GetLastError();
    	// Error message should be fomatted with LastError included
    	AfxMessageBox("LoadImage failed");
    	return FALSE;
    if (!m_Bitmap.Attach(hImage)) {
    	AfxMessageBox("Bitmap could not be attached");
    	return FALSE;
    return TRUE;
  4. Add the following to the header for the document:

    	HBITMAP GetHandle() const {return (HBITMAP)m_Bitmap.m_hObject;};
    	void SelectOldBitmap(CDC *pDCMem) {pDCMem->SelectObject(m_pOldBitmap);};
    	void SelectBitmap(CDC *pDCMem)
    	int GetBitmap(BITMAP* pBitMap) {return m_Bitmap.GetBitmap(pBitMap);};
    	CBitmap m_Bitmap;
    	CBitmap* m_pOldBitmap;
  5. Use ClassWizard to remove processing for OnInitialUpdate in your view class and delete the function from the code

  6. Add to the view constructor:

    SetScrollSizes(MM_TEXT, CSize(0, 0));
  7. Use ClassWizard to add processing for OnUpdate in your view class and add the following to it:

    	CBMPLookDoc* pDoc = GetDocument();
    	CFrameWnd* pParentFrame = GetParentFrame();
    	BITMAP BitMap;
    if (!pDoc->GetHandle())
    SetScrollSizes(MM_TEXT, CSize(BitMap.bmWidth, BitMap.bmHeight));
  8. Finally, replace the OnDraw processing with the following:

    	CBMPLookDoc* pDoc = GetDocument();
    	BITMAP BitMap;
    	CDC DCMem;
    // Do not call CWnd::OnPaint() for painting messages
    if (!pDoc->GetHandle())
    if (!DCMem.CreateCompatibleDC(pDC))
    	TRACE0("DCMem.CreateCompatibleDC failed\n");
    if (!pDC->BitBlt(0, 0, BitMap.bmWidth, BitMap.bmHeight, &DCMem, 0, 0, SRCCOPY))
    	TRACE0("BitBlt failed\n");

You should also modify the "<filterName>" and "<filterExt>" in the Document Template String to use appropriate file extensions and ".bmp" for the file extension. See CDocTemplate::GetDocString and Microsoft Knowledge Base article "INFO: Format of the Document Template String" (Article ID: Q129095) for information on Document Template Strings. The string resource id for the string is usually 129.

The LR_CREATEDIBSECTION flag in the LoadImage function can probably be removed to improve performance, but the documentaion for this and many, many other things need to be studied because bitmaps and graphics are quite complex.


To go beyond this very simple sample, the MFC DIBLook sample is an example of the type of sample that is too complicated to get just the basics from but does show most of the fancier stuff that should be done.

There is an abundance of documentation to wade through in the section on Bitmaps in the GDI portion of Graphics and Multimedia Services in the Platform SDK documentation. For Windows 9X, in the Windows Base Services chapter of the Platform SDK there is a Windows 95 Graphics Device Interface topic in Windows 95 System Limitations in About Windows 95 in Windows 95 Features.

The Multimedia Technical Article DIBs and Their Use might be helpful.

In the Microsoft Systems Journal Volume 12 Number 1 is the article More Fun with MFC: DIBs, Palettes, Subclassing, and a Gamut of Reusable Goodies.

The following are Microsoft Knowledge Base articles (the last two are from the Win16 knowledge base):

Q94326 SAMPLE: 16 and 32 Bits-Per-Pel Bitmap Formats
Q158898 How To Use LoadImage() to Read a BMP File
Q124947 Retrieving Palette Information from a Bitmap Resource
Q159649 PARSEBIT.EXE Directly Accesses the Bits of a DIB Section
Q67883 How to Use a DIB Stored as a Windows Resource
Q83034 SAMPLE: Reading and Converting Between the Three GDI Resources
Q81498 SAMPLE: DIBs and Their Uses



  • Nice

    Posted by Jackson Grey on 04/06/2017 01:39pm

    This is nice. I usually open my .bmp files like this: but this works good too. Nice work.

  • Simple BMP Viewer

    Posted by Legacy on 10/14/2003 12:00am

    Originally posted by: peter

    I downloaded the code and it works. However, when the window is resized - the image blinks terribly. I downloaded also an SDK code which is parralel to this onw and there is no blinkig.
    But the code uses an intermediate memory DC - so where the blinking comes from?
    Does some of MFC classes erase the client area?
    Is this caused by a slow CPU?

    Any suggestion?

  • Use MFC to open bitmap

    Posted by Legacy on 06/04/2003 12:00am

    Originally posted by: james

    from above question (displaying bitmap),

    IDB_YOURBITMAPRESOURCE is the ID of a bitmap that you've inserted into your resources

    How to insert the bitmap into my resourse and get it's ID ?
    i got a bitmap file already and i need to use MFC to open it , can you give me some idea to write the program ? i really face big trouble in it . Thanks ...

  • comm

    Posted by Legacy on 02/27/2003 12:00am

    Originally posted by: arun

    how to do the same using c++

  • I have a Array of COLORREF in Microsoft SDK, How do I convert it into Bitmap Image ??

    Posted by Legacy on 01/08/2003 12:00am

    Originally posted by: Bhavin

    I have a Array of COLORREF in Microsoft SDK, How do I convert it into Bitmap Image ??

    I have captured screen by GetPixel and stored in an array of COLORREF, Ineed it to convert to Bitmap !! Can u help me if any Code or ideas available..

    Do mail me emergently, Please ...

  • Simple bitmap Viewer

    Posted by Legacy on 12/29/2002 12:00am

    Originally posted by: karpurgaur

    Is there a source code to download so I can study this material especially LoadBitmap function

  • Change the bitmap by a timer

    Posted by Legacy on 12/17/2002 12:00am

    Originally posted by: candon

    I downloaded your exemple : GREAT !
    I have this problem: I want change the image by a TIMER or by a button.
    I'm not able to apply this modification at your project.
    Can you help me please ?
    Have you an exemple ?

  • Need to build a App just like Ms Paint

    Posted by Legacy on 12/03/2002 12:00am

    Originally posted by: Shiran


    could some one advice me on how I could build an application just like paint.

    I need to develope an program that can actually be able to open up a bmp file and then edit it just like paint. draw with pencil and may be add text comments on a certain part of the picture. Most import is that I should be able to run this program through my own application.

    Some one please advice me on how I could do this. also what would be the best tools that I could use.

    Thank you,

  • Why do none of these tutorials work on Windows ce

    Posted by Legacy on 09/04/2002 12:00am

    Originally posted by: Karl Wallace

    I have tried a number of tutorials displaying images in CScrollView, and they all display the image fine until you start to move the scroll bar. Then you get view full of replicated slivers of the image. What's up with that?

  • how to import an network in view window to dialog box?

    Posted by Legacy on 09/02/2002 12:00am

    Originally posted by: anu


    i have drawn a network in the view window.. i want this network drawn in the view window to get displayed in a dialog box. is that posssible?. can u give me some source code?.

    awaiting for ur reply.

  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

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