Home

Views Topics: Displaying a Bitmap

 

Introduction

Displaying a bitmap in a CView or CScrollView-based application can be fairly easy if you are using a bitmap created as a resource. What makes this easy is that you already know what picture the user would use and you may have provided only a (limited) set of pictures. In some applications, you may want to let the user select the picture to display. To do this, you would use a File Dialog that allows the user to select a picture as a file and them you can display it.

Practical Learning Practical Learning: Displaying a Bitmap in a View

 

  1. Start Microsoft Visual C++ and create an MFC Application named ShowPicture1
  2. Create it as a Single Document
  3. Base the view class on CScrollView and click Finish
  4. In the Class View, right-click the name of the view class -> Add -> Add Variable...
  5. Create a private CString variable named strFilename and click Finish
  6. In the Resource View, double-click the IDR_MAINFRAME menu
  7. Right the Open menu item and click Add Event Handler...
  8. In the Class List, select the view class
  9. Click Add and Edit
  10. Implement the event as follows:
     
    void CShowPicture1View::OnFileOpen()
    {
    	// TODO: Add your command handler code here
    	TCHAR strFilter[] = { TEXT("Picture Files (*.bmp)|*.bmp||") };
    	CFileDialog dlg(TRUE, TEXT(".bmp"), NULL, 0, strFilter);
    
    	if( dlg.DoModal() == IDOK )
    	{
    		strFilename = dlg.GetFileName();
    		Invalidate();
    	}
    }
  11. Access the OnDraw event (of the view class) and change its as follows:
     
    void CShowPicture1View::OnDraw(CDC* pDC)
    {
    	CShowPicture1Doc* pDoc = GetDocument();
    	ASSERT_VALID(pDoc);
    	if (!pDoc)
    		return;
    
    	// TODO: add draw code for native data here
    	if( strFilename != "" )
    	{
    		BITMAP bmpProperties;
    
    		// Create a bitmap handle using the name of the file
    		HBITMAP bmpHandle = (HBITMAP)LoadImage(NULL,
    			                               strFilename,
    					               IMAGE_BITMAP,
    					               0,
    						       0,
    						       LR_LOADFROMFILE);
    
    		CBitmap bmpPicture;
    		CDC mdcPicture;
    
    	// Convert the Win32 bitmap handle into an MFC bitmap object
    		CBitmap *bmpFromHandle = bmpPicture.FromHandle(bmpHandle);
    		bmpPicture.Attach(bmpHandle);
    
    	// Call the Win32 GetObject() function to get the properties
    		// of the bitmap and store them in a BITMAP structure
    		::GetObject(bmpPicture,
    			    sizeof(bmpProperties),
    			    &bmpProperties);
    
    		// Create a compatible device context
    		mdcPicture.CreateCompatibleDC(pDC);
    		// Select the bitmap into the device context
    		CBitmap * bmpPrevious = 
    				mdcPicture.SelectObject(bmpFromHandle);
    
    		// Using the dimensions store in the BITMAP object,
    		// display the picture
    		pDC->BitBlt(0, 0,
    			    bmpProperties.bmWidth, bmpProperties.bmHeight,
    			    &mdcPicture, 0, 0, SRCCOPY);
    
    		// Get the dimensions of the new picture
    		SIZE sizeTotal;
    		sizeTotal.cx = bmpProperties.bmWidth;
    		sizeTotal.cy = bmpProperties.bmHeight;
    		// Change the scrolling area/dimensions of the view
    		SetScrollSizes(MM_TEXT, sizeTotal);
    
    		// Restore the bitmap
    		pDC->SelectObject(bmpPrevious);
    		// Delete the Win32 bitmap handle
    		DeleteObject(bmpHandle);
    	}
    }
  12. Execute the application
     
  13. Close the application

Related Topics:

 

Home