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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Changing pixel color on a bitmap

Changing pixel color on a bitmap

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphics
6 Posts 2 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.
  • V Offline
    V Offline
    vcplusplus
    wrote on last edited by
    #1

    I have a button on my CFormView with a bitmap. How can I change the color of each individual pixel in the bitmap?

    R 1 Reply Last reply
    0
    • V vcplusplus

      I have a button on my CFormView with a bitmap. How can I change the color of each individual pixel in the bitmap?

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      dwccgc wrote: How can I change the color of each individual pixel in the bitmap? Select it into a device context and use SetPixel() or SetPixelV(), the latter being the fastest.

      Ryan

      "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

      V 1 Reply Last reply
      0
      • R Ryan Binns

        dwccgc wrote: How can I change the color of each individual pixel in the bitmap? Select it into a device context and use SetPixel() or SetPixelV(), the latter being the fastest.

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        V Offline
        V Offline
        vcplusplus
        wrote on last edited by
        #3

        I need to get the current color before I use SetPixel(..), but I cannot get GetPixel() to work. pDc->GetPixel() returns -1. Maybe I am missing something. Note: m_ExitButtonCtrl is of type CButton. hbmButtonExit is of type HBITMAP.

        void CMyFormView::OnInitialUpdate()
        {
        CFormView::OnInitialUpdate();

        // Load BitMap
        hbmButtonExit = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB\_BUTTON\_EXIT));
        if (hbmButtonExit != NULL)
        {
           m\_ExitButtonCtrl.SetBitmap(hbmButtonExit);
           CDC \*pDc = m\_ExitButtonCtrl.GetDC();
           if (pDc != NULL)
           {
               for (INT nX = 0; nX < 69; nX++)
               {
                   for (INT nY = 0; nY < 47; nY++)
                   {
                      COLORREF OrgColor = **pDc->GetPixel(nX, nY);**
                   }
               }
           }
        }
        return;
        

        }

        R 1 Reply Last reply
        0
        • V vcplusplus

          I need to get the current color before I use SetPixel(..), but I cannot get GetPixel() to work. pDc->GetPixel() returns -1. Maybe I am missing something. Note: m_ExitButtonCtrl is of type CButton. hbmButtonExit is of type HBITMAP.

          void CMyFormView::OnInitialUpdate()
          {
          CFormView::OnInitialUpdate();

          // Load BitMap
          hbmButtonExit = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB\_BUTTON\_EXIT));
          if (hbmButtonExit != NULL)
          {
             m\_ExitButtonCtrl.SetBitmap(hbmButtonExit);
             CDC \*pDc = m\_ExitButtonCtrl.GetDC();
             if (pDc != NULL)
             {
                 for (INT nX = 0; nX < 69; nX++)
                 {
                     for (INT nY = 0; nY < 47; nY++)
                     {
                        COLORREF OrgColor = **pDc->GetPixel(nX, nY);**
                     }
                 }
             }
          }
          return;
          

          }

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          You need to select the bitmap into a device context.

          void CMyFormView::OnInitialUpdate()
          {
          CFormView::OnInitialUpdate();
          hbmButtonExit = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BUTTON_EXIT));
          if (hbmButtonExit != NULL)
          {
          CClientDC dc(this);
          CDC memDC;
          memDC.CreateCompatibleDC(&dc);
          CBitmap *pBitmap = memDC.SelectObject(CBitmap::FromHandle(hbmButtonExit));
          for (INT nX = 0; nX < 69; nX++)
          {
          for (INT nY = 0; nY < 47; nY++)
          {
          COLORREF OrgColor = memDC.GetPixel(nX, nY);
          // Do whatever else here
          }
          }
          memDC.SelectObject(pBitmap);
          m_ExitButtonCtrl.SetBitmap(hbmButtonExit);
          }
          }

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          V 1 Reply Last reply
          0
          • R Ryan Binns

            You need to select the bitmap into a device context.

            void CMyFormView::OnInitialUpdate()
            {
            CFormView::OnInitialUpdate();
            hbmButtonExit = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BUTTON_EXIT));
            if (hbmButtonExit != NULL)
            {
            CClientDC dc(this);
            CDC memDC;
            memDC.CreateCompatibleDC(&dc);
            CBitmap *pBitmap = memDC.SelectObject(CBitmap::FromHandle(hbmButtonExit));
            for (INT nX = 0; nX < 69; nX++)
            {
            for (INT nY = 0; nY < 47; nY++)
            {
            COLORREF OrgColor = memDC.GetPixel(nX, nY);
            // Do whatever else here
            }
            }
            memDC.SelectObject(pBitmap);
            m_ExitButtonCtrl.SetBitmap(hbmButtonExit);
            }
            }

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            V Offline
            V Offline
            vcplusplus
            wrote on last edited by
            #5

            Thanks for the help.:)

            R 1 Reply Last reply
            0
            • V vcplusplus

              Thanks for the help.:)

              R Offline
              R Offline
              Ryan Binns
              wrote on last edited by
              #6

              You're welcome :)

              Ryan

              "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

              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