CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Apr 2005
    Posts
    1,828

    Displaying a Bitmap

    I have an MFCActiveX control, in which there is a ctrl class created by the wizard. Within that class there is a function named ondraw like this..

    Code:
    void CGregTestCtrl::OnDraw(
    			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
    {
    	// TODO: Replace the following code with your own drawing code.
    	 pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(GRAY_BRUSH)));
    	pdc->Ellipse(rcBounds);	
    
    }
    When I use this control anywhere in an application it displays an Ellipse. I want to display a Bitmap and some written text instead of that ellipse. So what all changes do I have to make in OnDraw() function??

  2. #2
    Join Date
    Jul 2005
    Location
    india
    Posts
    17

    Re: Displaying a Bitmap

    Hi,

    If you don't want to see Ellipse comment the pregenarated code in OnDraw function.

    If you want to display bitmap first load that in to memory and by using BitBlt() you can draw bitmap

    http://www.desktoppack.com
    c0mgu1

  3. #3
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Displaying a Bitmap

    Quote Originally Posted by c0mgu1
    Hi,

    If you don't want to see Ellipse comment the pregenarated code in OnDraw function.

    If you want to display bitmap first load that in to memory and by using BitBlt() you can draw bitmap

    http://www.desktoppack.com
    I have sluggish knowledge about that. Can you please write or show me the code of Bitmap??

  4. #4
    Join Date
    May 2005
    Posts
    364

    Re: Displaying a Bitmap

    Quote Originally Posted by maverick786us
    I have sluggish knowledge about that. Can you please write or show me the code of Bitmap??
    HI,

    U CAN USE THIS CODE EXAMPLE FOR DISPLAYING BITMAPS ON SCREEN. BUT AS SAID BEFORE U SHOULD HAVE IT IN THE RESOURCES (i.e., IDB_BITMAP1)

    /////////////// BITMAP /////////////
    CBitmap bitmap;
    CDC dcMemory;
    bitmap.LoadBitmap(IDB_BITMAP1);
    dcMemory.CreateCompatibleDC(&dcMemory);
    dcMemory.SelectObject(&bitmap);
    dc.BitBlt(120, 100, 161, 183, &dcMemory, 0, 0, SRCCOPY);

    ////////////////// END ///////////////////

  5. #5
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Displaying a Bitmap

    I used your code but the application is crashing here it is.

    Code:
    void CGregTestCtrl::OnDraw(
    			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
    {
    	CBitmap bmp;
    	bmp.LoadBitmap (IDB_BMP_LOGO);
    	pdc->CreateCompatibleDC (pdc);
    	pdc->SelectObject (&bmp);
    	pdc->BitBlt(120, 100, 161, 183, pdc, 0, 0, SRCCOPY);
    }
    I think the problem lies over here
    Code:
    pdc->CreateCompatibleDC (pdc);
    IDB_BMP_LOGO is the bitmap I imported
    Last edited by maverick786us; February 10th, 2007 at 05:08 AM.

  6. #6
    Join Date
    May 2005
    Posts
    364

    Unhappy Re: Displaying a Bitmap

    I think the problem lies over here
    Code:
    pdc->CreateCompatibleDC (pdc);
    IDB_BMP_LOGO is the bitmap I imported[/QUOTE]

    Please check the size of the bitmap ur using.

    Also u should see compatibility issues as mentioned in msdn:

    See the CDC::BitBlt member function for information regarding bit-block transfers between device contexts. To determine whether a device context supports raster operations, see the RC_BITBLT raster capability in the member function

  7. #7
    Join Date
    Mar 2005
    Location
    Romania,Cluj-Napoca
    Posts
    1,073

    Re: Displaying a Bitmap

    Quote Originally Posted by maverick786us
    I used your code but the application is crashing here it is.

    Code:
    void CGregTestCtrl::OnDraw(
    			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
    {
    	CBitmap bmp;
    	bmp.LoadBitmap (IDB_BMP_LOGO);
    	pdc->CreateCompatibleDC (pdc);
    	pdc->SelectObject (&bmp);
    	pdc->BitBlt(120, 100, 161, 183, pdc, 0, 0, SRCCOPY);
    }
    I think the problem lies over here
    Code:
    pdc->CreateCompatibleDC (pdc);
    IDB_BMP_LOGO is the bitmap I imported

    I think this line is wrong.
    Code:
    pdc->CreateCompatibleDC (pdc);
    //Creates a memory device context that is compatible with the device specified by pDC.
    So you make compatible the pdc to pdc????

    Plese look at an example taken from the MSDN:
    Code:
    void CBlat2View::OnDraw(CDC* pDC)
    {
       CBlat2Doc* pDoc = GetDocument();
       ASSERT_VALID(pDoc);
    
       // load IDB_BITMAP1 from our resources
       CBitmap bmp;
       if (bmp.LoadBitmap(IDB_BITMAP1))
       {
          // Get the size of the bitmap
          BITMAP bmpInfo;
          bmp.GetBitmap(&bmpInfo);
    
          // Create an in-memory DC compatible with the
          // display DC we're using to paint
          CDC dcMemory;
          dcMemory.CreateCompatibleDC(pDC);
    
          // Select the bitmap into the in-memory DC
          CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);
    
          // Find a centerpoint for the bitmap in the client area
          CRect rect;
          GetClientRect(&rect);
          int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
          int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;
    
          // Copy the bits from the in-memory DC into the on-
          // screen DC to actually do the painting. Use the centerpoint
          // we computed for the target offset.
          pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 
             0, 0, SRCCOPY);
    
          dcMemory.SelectObject(pOldBitmap);
       }
       else
          TRACE0("ERROR: Where's IDB_BITMAP1?\n");
    }

    Best regards,
    Gili
    Please use code tags [code] [/code]

    We would change the world, but God won't give us the sourcecode..
    Undocumented futures are fun and useful....
    ___
    ______
    Gili

  8. #8
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Displaying a Bitmap

    Thanks for the help Gill. It is successfully creating Compitable memory DC. But now the problem is coming in selecting the bitmap into the memory DC. Over here it crashes

    Code:
    CBitmap * pOldBitmap = dcMemory.SelectObject (&bmp);

  9. #9
    Join Date
    Mar 2005
    Location
    Romania,Cluj-Napoca
    Posts
    1,073

    Re: Displaying a Bitmap

    Hi maverick786us,


    what sample did you used ? In your code? Can you please show your actually code?

    Thanks,
    Gili
    Please use code tags [code] [/code]

    We would change the world, but God won't give us the sourcecode..
    Undocumented futures are fun and useful....
    ___
    ______
    Gili

  10. #10
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Displaying a Bitmap

    Here is the code I have attached. In order to test it you need to register it and then open Micorsoft word>Insert Objects>Greg Digital Sign.

    I have also attached the code for registration.

    Thanks once again
    Attached Files Attached Files
    Last edited by maverick786us; January 12th, 2011 at 03:31 AM.

  11. #11
    Join Date
    Mar 2005
    Location
    Romania,Cluj-Napoca
    Posts
    1,073

    Re: Displaying a Bitmap

    Quote Originally Posted by maverick786us
    Here is the code I have attached. In order to test it you need to register it and then open Micorsoft word>Insert Objects>Greg Digital Sign.

    I have also attached the code for registration.

    Thanks once again
    Hi maverick786us try to use the following code:

    Code:
    void CGregTestCtrl::OnDraw(CDC* pDC, const CRect& rcBounds, const CRect& rcInvalid)
    {
    	pDC->Rectangle (rcBounds);
    	CBitmap bmp;
    	CClientDC dc(NULL);
    	if (bmp.LoadBitmap (IDB_BMP_LOGO))
    	{
    		// Get the size of the bitmap
    		BITMAP bmpInfo;
    		bmp.GetBitmap(&bmpInfo);
    
    		// Create an in-memory DC compatible with the
    		// display DC we're using to paint
    		CDC dcMemory;
    		if ( dcMemory.CreateCompatibleDC(NULL) )
    		{
    			// Select the bitmap into the in-memory DC
    			CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);
    
    
    			// Copy the bits from the in-memory DC into the on-
    			// screen DC to actually do the painting. Use the centerpoint
    			// we computed for the target offset.
    			pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 
    				0, 0, SRCCOPY);
    
    			dcMemory.SelectObject(pOldBitmap);
    		}
    	}
    	else
    	{
    		TRACE ("ERROR: Where's IDB_BITMAP?\n");
    	}	
    }
    Your original example worked in the ActiveX Control Test container utility, but not in MSWord as an object.
    The the COleObject has a null m_hWnd this means you can't use the GetClientRect() and also the CreateCompatibleDC(pdc) ( returned NULL ) failed . So use it in this way:


    Code:
     if ( dcMemory.CreateCompatibleDC(NULL) )
    {
    
    .................
    
    }

    Best Regards,
    Gili

    PS:
    I'm not to familiar with OLE
    Also attached the file where I made the modifications.
    Attached Files Attached Files
    Please use code tags [code] [/code]

    We would change the world, but God won't give us the sourcecode..
    Undocumented futures are fun and useful....
    ___
    ______
    Gili

  12. #12
    Join Date
    May 2007
    Posts
    1

    Re: Displaying a Bitmap

    i know already how to display a bitmap using the resource. i want to know wat if i want to display a bitmap file without the use of resource? just directly from open dialog box.

  13. #13
    Join Date
    May 2005
    Posts
    4,954

    Re: Displaying a Bitmap

    Quote Originally Posted by go939
    i know already how to display a bitmap using the resource. i want to know wat if i want to display a bitmap file without the use of resource? just directly from open dialog box.
    Is your problem is to load the image from file? or get the name from the open dialog box?

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  14. #14
    Join Date
    Jul 2005
    Location
    E: 120°.6, N: 31°.3′
    Posts
    795

    Re: Displaying a Bitmap

    You have to clarify according to golanshahar's suggestion first. and tell us what exactly you want. in addition, pls. see the below:
    Code:
    BOOL CMyBitmap::LoadBitmap(LPCTSTR szFilename)
    {
    	ASSERT(szFilename);
    	DeleteObject();
    
    	HBITMAP hBitmap = NULL;
    	hBitmap = (HBITMAP)LoadImage(NULL, szFilename, IMAGE_BITMAP, 0, 0,
    		LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
    	return Attach(hBitmap);
    }
    Last edited by sunny_sz; May 23rd, 2007 at 03:23 AM.
    Little by little one goes far
    Keep moving.......!
    Nothing is impossible !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured