Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Monochrome Bitmap Issue

Monochrome Bitmap Issue

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++comgraphics
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CoffeeAddict19
    wrote on last edited by
    #1

    I'm having a problem with memory device contexts and bitmaps in MFC. I've gotten to the point to where I can create and use them, but natrually they are all in black in white. See here: http://img.photobucket.com/albums/v50/blackdwarf/piegraphcontrol.jpg[^] With a CreateCompatibleDC(NULL) call, a monochrome bitmap gets created for and selected into the CDC. When I actually create my bitmap based off of that CDC, I'm stuck with the monochrome. So my question is, how do I initialize the bitmaps with color outside of OnPaint when I can't just use CPaintDC? Here is some of the code... Creating the two bitmaps without a CPaintDC (where I think the problem is): BOOL CPieGraph::Create(CWnd* pParentWnd, const RECT& rect, UINT nID, DWORD dwStyle /*=WS_VISIBLE*/) { BOOL OverallSuccess = FALSE; BOOL CreateResult = FALSE, TextDCResult = FALSE, PieDCResult = FALSE; int SelectText = 0, SelectPie = 0; CreateResult = CWnd::Create(CPIEGRAPH_CLASSNAME, _T(""), dwStyle, rect, pParentWnd, nID); TextDCResult = TextMemDC.CreateCompatibleDC(NULL); PieDCResult = PieMemDC.CreateCompatibleDC(NULL); SelectText = m_textBitmap.CreateCompatibleBitmap(&TextMemDC, 250, 20); SelectPie = m_pieBitmap.CreateCompatibleBitmap(&PieMemDC, 300, 300); OldtextBitmap = TextMemDC.SelectObject(&m_textBitmap); OldpieBitmap = PieMemDC.SelectObject(&m_pieBitmap); TextMemDC.SetBkColor(RGB(255,255,255)); PieMemDC.SetBkColor(RGB(255,255,255)); if((CreateResult == TRUE) && (TextDCResult == TRUE) && (PieDCResult == TRUE) && (SelectText != 0) && (SelectPie != 0)) { OverallSuccess = TRUE; ControlRect = rect; } else OverallSuccess = FALSE; return OverallSuccess; } And OnPaintDC:

    afx_msg void CPieGraph::OnPaint()
    {
    	CPaintDC dc(this);
    
    	CDC ControlMemDC;
    	CBitmap ControlBitmap;
    	CBitmap* OldBitmap = NULL;
    
    	ControlMemDC.CreateCompatibleDC(&dc);
    	ControlBitmap.CreateCompatibleBitmap(&ControlMemDC, 300, 360);
    	OldBitmap = ControlMemDC.SelectObject(&ControlBitmap);
    	ControlMemDC.SetBkColor(RGB(255,255,255));
    
    	if(UpdateNeeded == true)
    	{
    		RedrawPieText(TextMemDC);
    		RedrawPieChart(PieMemDC);
    		ControlMemDC.BitBlt(0, 0, 250, 20, &TextMemDC, 0, 0, SRCCOPY);
    		ControlMemDC.BitBlt(0, 25, MainPieGraph.radius * 2, 
    			MainPieGraph.radius * 2, &PieMemDC,
    
    B M 2 Replies Last reply
    0
    • C CoffeeAddict19

      I'm having a problem with memory device contexts and bitmaps in MFC. I've gotten to the point to where I can create and use them, but natrually they are all in black in white. See here: http://img.photobucket.com/albums/v50/blackdwarf/piegraphcontrol.jpg[^] With a CreateCompatibleDC(NULL) call, a monochrome bitmap gets created for and selected into the CDC. When I actually create my bitmap based off of that CDC, I'm stuck with the monochrome. So my question is, how do I initialize the bitmaps with color outside of OnPaint when I can't just use CPaintDC? Here is some of the code... Creating the two bitmaps without a CPaintDC (where I think the problem is): BOOL CPieGraph::Create(CWnd* pParentWnd, const RECT& rect, UINT nID, DWORD dwStyle /*=WS_VISIBLE*/) { BOOL OverallSuccess = FALSE; BOOL CreateResult = FALSE, TextDCResult = FALSE, PieDCResult = FALSE; int SelectText = 0, SelectPie = 0; CreateResult = CWnd::Create(CPIEGRAPH_CLASSNAME, _T(""), dwStyle, rect, pParentWnd, nID); TextDCResult = TextMemDC.CreateCompatibleDC(NULL); PieDCResult = PieMemDC.CreateCompatibleDC(NULL); SelectText = m_textBitmap.CreateCompatibleBitmap(&TextMemDC, 250, 20); SelectPie = m_pieBitmap.CreateCompatibleBitmap(&PieMemDC, 300, 300); OldtextBitmap = TextMemDC.SelectObject(&m_textBitmap); OldpieBitmap = PieMemDC.SelectObject(&m_pieBitmap); TextMemDC.SetBkColor(RGB(255,255,255)); PieMemDC.SetBkColor(RGB(255,255,255)); if((CreateResult == TRUE) && (TextDCResult == TRUE) && (PieDCResult == TRUE) && (SelectText != 0) && (SelectPie != 0)) { OverallSuccess = TRUE; ControlRect = rect; } else OverallSuccess = FALSE; return OverallSuccess; } And OnPaintDC:

      afx_msg void CPieGraph::OnPaint()
      {
      	CPaintDC dc(this);
      
      	CDC ControlMemDC;
      	CBitmap ControlBitmap;
      	CBitmap* OldBitmap = NULL;
      
      	ControlMemDC.CreateCompatibleDC(&dc);
      	ControlBitmap.CreateCompatibleBitmap(&ControlMemDC, 300, 360);
      	OldBitmap = ControlMemDC.SelectObject(&ControlBitmap);
      	ControlMemDC.SetBkColor(RGB(255,255,255));
      
      	if(UpdateNeeded == true)
      	{
      		RedrawPieText(TextMemDC);
      		RedrawPieChart(PieMemDC);
      		ControlMemDC.BitBlt(0, 0, 250, 20, &TextMemDC, 0, 0, SRCCOPY);
      		ControlMemDC.BitBlt(0, 25, MainPieGraph.radius * 2, 
      			MainPieGraph.radius * 2, &PieMemDC,
      
      B Offline
      B Offline
      Bram van Kampen
      wrote on last edited by
      #2

      Don't know an awfull lot about the issues, but it appears to be at first sight to be a problem about loss of information.It would appear to me that you first create a monochrome bitmap from a source, which you select into the CDC. At that stage the Die is cast as far as the CDC is concerned. The Monochrome bitmap is all it knows about. The CDC has built a Monochrome picture which it knows your Device can display. All Colour Info has been discarded. If at a later stage you want the Bitmap in colour, you have to start again from scratch in the same or another CDC, This time indicating yont to keep the colour. Mind you,to do so you could recycle most of the code. Just leave an indication somewhere about what way you want it, Monocrome or Colour. A Device Context is a Uniform Construct that Converts things like bitmaps etc into something your Hardware can display. (That's the way device-independence is Implemented). Hope this is Usefull. :)

      LateNightsInNewry

      1 Reply Last reply
      0
      • C CoffeeAddict19

        I'm having a problem with memory device contexts and bitmaps in MFC. I've gotten to the point to where I can create and use them, but natrually they are all in black in white. See here: http://img.photobucket.com/albums/v50/blackdwarf/piegraphcontrol.jpg[^] With a CreateCompatibleDC(NULL) call, a monochrome bitmap gets created for and selected into the CDC. When I actually create my bitmap based off of that CDC, I'm stuck with the monochrome. So my question is, how do I initialize the bitmaps with color outside of OnPaint when I can't just use CPaintDC? Here is some of the code... Creating the two bitmaps without a CPaintDC (where I think the problem is): BOOL CPieGraph::Create(CWnd* pParentWnd, const RECT& rect, UINT nID, DWORD dwStyle /*=WS_VISIBLE*/) { BOOL OverallSuccess = FALSE; BOOL CreateResult = FALSE, TextDCResult = FALSE, PieDCResult = FALSE; int SelectText = 0, SelectPie = 0; CreateResult = CWnd::Create(CPIEGRAPH_CLASSNAME, _T(""), dwStyle, rect, pParentWnd, nID); TextDCResult = TextMemDC.CreateCompatibleDC(NULL); PieDCResult = PieMemDC.CreateCompatibleDC(NULL); SelectText = m_textBitmap.CreateCompatibleBitmap(&TextMemDC, 250, 20); SelectPie = m_pieBitmap.CreateCompatibleBitmap(&PieMemDC, 300, 300); OldtextBitmap = TextMemDC.SelectObject(&m_textBitmap); OldpieBitmap = PieMemDC.SelectObject(&m_pieBitmap); TextMemDC.SetBkColor(RGB(255,255,255)); PieMemDC.SetBkColor(RGB(255,255,255)); if((CreateResult == TRUE) && (TextDCResult == TRUE) && (PieDCResult == TRUE) && (SelectText != 0) && (SelectPie != 0)) { OverallSuccess = TRUE; ControlRect = rect; } else OverallSuccess = FALSE; return OverallSuccess; } And OnPaintDC:

        afx_msg void CPieGraph::OnPaint()
        {
        	CPaintDC dc(this);
        
        	CDC ControlMemDC;
        	CBitmap ControlBitmap;
        	CBitmap* OldBitmap = NULL;
        
        	ControlMemDC.CreateCompatibleDC(&dc);
        	ControlBitmap.CreateCompatibleBitmap(&ControlMemDC, 300, 360);
        	OldBitmap = ControlMemDC.SelectObject(&ControlBitmap);
        	ControlMemDC.SetBkColor(RGB(255,255,255));
        
        	if(UpdateNeeded == true)
        	{
        		RedrawPieText(TextMemDC);
        		RedrawPieChart(PieMemDC);
        		ControlMemDC.BitBlt(0, 0, 250, 20, &TextMemDC, 0, 0, SRCCOPY);
        		ControlMemDC.BitBlt(0, 25, MainPieGraph.radius * 2, 
        			MainPieGraph.radius * 2, &PieMemDC,
        
        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        To add to LateNightsInNewry's reply... To fix it... When you call CreateCompatibleBitmap(), you should use the screen or paint DC, not the memory DC. The memory DC you created with CreateCompatibleDC() is the one with a monochrome bitmap selected into it. If you create a bitmap compatible with that, it will also be monochrome. Outside of OnPaint, you can get a screen DC with ::GetDC(0) or something like CDC ScreenDC; ScreenDC.CreateCompatibleDC(0); Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        C 1 Reply Last reply
        0
        • M Mark Salsbery

          To add to LateNightsInNewry's reply... To fix it... When you call CreateCompatibleBitmap(), you should use the screen or paint DC, not the memory DC. The memory DC you created with CreateCompatibleDC() is the one with a monochrome bitmap selected into it. If you create a bitmap compatible with that, it will also be monochrome. Outside of OnPaint, you can get a screen DC with ::GetDC(0) or something like CDC ScreenDC; ScreenDC.CreateCompatibleDC(0); Mark

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          C Offline
          C Offline
          CoffeeAddict19
          wrote on last edited by
          #4

          Thanks, both of you. I think it'll work. I'll try it tommorow when I wake up.

          M 1 Reply Last reply
          0
          • C CoffeeAddict19

            Thanks, both of you. I think it'll work. I'll try it tommorow when I wake up.

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            :java:

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            C 1 Reply Last reply
            0
            • M Mark Salsbery

              :java:

              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

              C Offline
              C Offline
              CoffeeAddict19
              wrote on last edited by
              #6

              Actually I just got a cup about 30 minutes ago. :) Unfortunatly, the problem still remains. :( I tried using GetDC:

              CDC* ScreenDC = NULL;
              CreateResult = CWnd::Create(CPIEGRAPH_CLASSNAME, _T(""), dwStyle, rect, pParentWnd, nID);
              
              TextDCResult = TextMemDC.CreateCompatibleDC(NULL);
              PieDCResult = PieMemDC.CreateCompatibleDC(NULL);
              
              ScreenDC = GetDC();
              ASSERT(ScreenDC == NULL);
              SelectText = m_textBitmap.CreateCompatibleBitmap(ScreenDC, 250, 20);
              SelectPie = m_pieBitmap.CreateCompatibleBitmap(ScreenDC, 300, 300);
              ReleaseDC(ScreenDC);
              

              At first the assert statement wasn't in there and I just got black and white, then I put it in and I got a debug assertation on the same line, indicating that ScreenDC was null, and that GetDC didn't do the job. Any idea why?

              C M 2 Replies Last reply
              0
              • C CoffeeAddict19

                Actually I just got a cup about 30 minutes ago. :) Unfortunatly, the problem still remains. :( I tried using GetDC:

                CDC* ScreenDC = NULL;
                CreateResult = CWnd::Create(CPIEGRAPH_CLASSNAME, _T(""), dwStyle, rect, pParentWnd, nID);
                
                TextDCResult = TextMemDC.CreateCompatibleDC(NULL);
                PieDCResult = PieMemDC.CreateCompatibleDC(NULL);
                
                ScreenDC = GetDC();
                ASSERT(ScreenDC == NULL);
                SelectText = m_textBitmap.CreateCompatibleBitmap(ScreenDC, 250, 20);
                SelectPie = m_pieBitmap.CreateCompatibleBitmap(ScreenDC, 300, 300);
                ReleaseDC(ScreenDC);
                

                At first the assert statement wasn't in there and I just got black and white, then I put it in and I got a debug assertation on the same line, indicating that ScreenDC was null, and that GetDC didn't do the job. Any idea why?

                C Offline
                C Offline
                CoffeeAddict19
                wrote on last edited by
                #7

                Well the assert statement was wrong...it should have been ASSERT(ScreenDC != NULL), but it is still not rendering color for some reason. Fonts, brushes, and pens work fine, just not color.

                M 1 Reply Last reply
                0
                • C CoffeeAddict19

                  Actually I just got a cup about 30 minutes ago. :) Unfortunatly, the problem still remains. :( I tried using GetDC:

                  CDC* ScreenDC = NULL;
                  CreateResult = CWnd::Create(CPIEGRAPH_CLASSNAME, _T(""), dwStyle, rect, pParentWnd, nID);
                  
                  TextDCResult = TextMemDC.CreateCompatibleDC(NULL);
                  PieDCResult = PieMemDC.CreateCompatibleDC(NULL);
                  
                  ScreenDC = GetDC();
                  ASSERT(ScreenDC == NULL);
                  SelectText = m_textBitmap.CreateCompatibleBitmap(ScreenDC, 250, 20);
                  SelectPie = m_pieBitmap.CreateCompatibleBitmap(ScreenDC, 300, 300);
                  ReleaseDC(ScreenDC);
                  

                  At first the assert statement wasn't in there and I just got black and white, then I put it in and I got a debug assertation on the same line, indicating that ScreenDC was null, and that GetDC didn't do the job. Any idea why?

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  CoffeeAddict19 wrote:

                  indicating that ScreenDC was null

                  Is Create() failing? Never mind, I see you fixed the ASSERT I steered you wrong with the CreateCompatibleDC, sorry :doh: *EDIT* I DID show you a way to do what you were already doing though :) This should work from anywhere:

                  CWindowDC ScreenDC(0);

                  CBitmap bitmap;
                  bitmap.CreateCompatibleBitmap(&ScreenDC, 20, 20);

                  //**Test - Check the resulting bitmap's format...
                  BITMAP bitmapstruct;
                  bitmap.GetObject(sizeof(BITMAP), &bitmapstruct);

                  You'll see in the BITMAP struct, the format matches the screen format. Try this:

                  CreateResult = CWnd::Create(CPIEGRAPH_CLASSNAME, _T(""), dwStyle, rect, pParentWnd, nID);
                  CWindowDC ScreenDC(0);
                  TextDCResult = TextMemDC.CreateCompatibleDC(&ScreenDC);
                  PieDCResult = PieMemDC.CreateCompatibleDC(&ScreenDC);
                  SelectText = m_textBitmap.CreateCompatibleBitmap(&ScreenDC, 250, 20);
                  SelectPie = m_pieBitmap.CreateCompatibleBitmap(&ScreenDC, 300, 300);

                  Mark -- modified at 14:54 Thursday 31st May, 2007

                  "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                  C 1 Reply Last reply
                  0
                  • C CoffeeAddict19

                    Well the assert statement was wrong...it should have been ASSERT(ScreenDC != NULL), but it is still not rendering color for some reason. Fonts, brushes, and pens work fine, just not color.

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    CoffeeAddict19 wrote:

                    but it is still not rendering color for some reason.

                    Hopefully my modified post below will help :)

                    "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                    1 Reply Last reply
                    0
                    • M Mark Salsbery

                      CoffeeAddict19 wrote:

                      indicating that ScreenDC was null

                      Is Create() failing? Never mind, I see you fixed the ASSERT I steered you wrong with the CreateCompatibleDC, sorry :doh: *EDIT* I DID show you a way to do what you were already doing though :) This should work from anywhere:

                      CWindowDC ScreenDC(0);

                      CBitmap bitmap;
                      bitmap.CreateCompatibleBitmap(&ScreenDC, 20, 20);

                      //**Test - Check the resulting bitmap's format...
                      BITMAP bitmapstruct;
                      bitmap.GetObject(sizeof(BITMAP), &bitmapstruct);

                      You'll see in the BITMAP struct, the format matches the screen format. Try this:

                      CreateResult = CWnd::Create(CPIEGRAPH_CLASSNAME, _T(""), dwStyle, rect, pParentWnd, nID);
                      CWindowDC ScreenDC(0);
                      TextDCResult = TextMemDC.CreateCompatibleDC(&ScreenDC);
                      PieDCResult = PieMemDC.CreateCompatibleDC(&ScreenDC);
                      SelectText = m_textBitmap.CreateCompatibleBitmap(&ScreenDC, 250, 20);
                      SelectPie = m_pieBitmap.CreateCompatibleBitmap(&ScreenDC, 300, 300);

                      Mark -- modified at 14:54 Thursday 31st May, 2007

                      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                      C Offline
                      C Offline
                      CoffeeAddict19
                      wrote on last edited by
                      #10

                      Thanks mark! I HAVE MADE COLOR! Check it out: http://img.photobucket.com/albums/v50/blackdwarf/piechart_color.jpg[^] Shouldn't take me long to get long of the pesky black area around the edges. Maybe I'll write a simple tutorial on making controls someday. There already is one, but another viewpoint couldn't hurt.

                      M 1 Reply Last reply
                      0
                      • C CoffeeAddict19

                        Thanks mark! I HAVE MADE COLOR! Check it out: http://img.photobucket.com/albums/v50/blackdwarf/piechart_color.jpg[^] Shouldn't take me long to get long of the pesky black area around the edges. Maybe I'll write a simple tutorial on making controls someday. There already is one, but another viewpoint couldn't hurt.

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #11

                        mmmm Pie! :) Cool!

                        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups