Simple BMP Viewer

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)
    	m_Bitmap.DeleteObject();
  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");
    DeleteContents();
    BeginWaitCursor();
    HBITMAP hImage = (HBITMAP)LoadImage(NULL, lpszPathName, IMAGE_BITMAP,
    	0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
    EndWaitCursor();
    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;
    	}
    SetModifiedFlag(FALSE);
    UpdateAllViews(NULL);
    return TRUE;
  4. Add the following to the header for the document:

    public:
    	HBITMAP GetHandle() const {return (HBITMAP)m_Bitmap.m_hObject;};
    	void SelectOldBitmap(CDC *pDCMem) {pDCMem->SelectObject(m_pOldBitmap);};
    	void SelectBitmap(CDC *pDCMem)
    		{m_pOldBitmap=pDCMem->SelectObject(&m_Bitmap);};
    	int GetBitmap(BITMAP* pBitMap) {return m_Bitmap.GetBitmap(pBitMap);};
    
    protected:
    	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())
    	return;
    pDoc->GetBitmap(&BitMap);
    SetScrollSizes(MM_TEXT, CSize(BitMap.bmWidth, BitMap.bmHeight));
    pParentFrame->RecalcLayout();
    ResizeParentToFit();
  8. Finally, replace the OnDraw processing with the following:

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

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.

References

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

History



Comments

  • 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?

    Reply
  • 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 ...

    Reply
  • comm

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

    Originally posted by: arun

    how to do the same using c++

    Reply
  • 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 ...

    Reply
  • 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

    Reply
  • 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 ?
    Thanks

    Reply
  • Need to build a App just like Ms Paint

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

    Originally posted by: Shiran

    Hi,

    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,
    Shiran~!

    Reply
  • 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?

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

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

    Originally posted by: anu

    Sir,

    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.

    Reply
  • how about visual c 6

    Posted by Legacy on 06/10/2002 12:00am

    Originally posted by: sam

    Could you tell me step by step for visual c 6.

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds