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. Problem with GetBitmapBits() in Win32

Problem with GetBitmapBits() in Win32

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicshelp
8 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.
  • A Offline
    A Offline
    ashishbhatt 0
    wrote on last edited by
    #1

    Hi All,, I am getting problem while getting bitmap bits from bitmap in Win32 app.And I got my bitmap data using the same thing in MFC in another app. I used below code in Win32...... int iWidth = rect.right - rect.left; int iHeight = rect.bottom - rect.top; HBITMAP new_Bitmap = CreateCompatibleBitmap(m_hrootdc,iWidth,iHeight); HBITMAP temp_Bitmap = (HBITMAP)SelectObject(m_hmemdc,new_Bitmap); // Capture screen into bitmap BOOL blitok = BitBlt( m_hmemdc, rect.left - m_bmrect.left , rect.top - m_bmrect.top , rect.right - rect.left, rect.bottom - rect.top, m_hrootdc, rect.left, rect.top, SRCCOPY); int datasize = iWidth*iHeight; COLORREF* lpClrData = (COLORREF*)LocalAlloc(LPTR, datasize * sizeof(COLORREF)); datasize = GetBitmapBits(temp_Bitmap,datasize * sizeof(COLORREF), lpClrData); Here rect is the some rectangle portion,here I got datasize as some no of bytes but in lpClrData buffer I don,t get anythig. And now I am using the same thing in MFC like bellow..... HDC memDC = CreateCompatibleDC(m_hrootdc); CDC* pScreenDC = CDC::FromHandle(m_hrootdc); CDC* pMemDC = CDC::FromHandle(memDC); int iWidth = rect.right - rect.left; int iHeight = rect.bottom - rect.top; CBitmap newBitmap; if (!newBitmap.CreateCompatibleBitmap(pScreenDC, iWidth,iHeight)) { MessageBox(NULL,CString("CreateCompatibleBitmap failed."),CString("Test"),0); return; } CBitmap* pOldBitmap = pMemDC->SelectObject(&newBitmap); pMemDC->BitBlt(0, 0, iWidth,iHeight, pScreenDC, 0, 0, SRCCOPY); CBitmap* pbitmap = pMemDC->GetCurrentBitmap(); int datasize = iWidth*iHeight; COLORREF* lpClrData = (COLORREF*)LocalAlloc(LPTR, datasize * sizeof(COLORREF)); datasize = pbitmap->GetBitmapBits(datasize * sizeof(COLORREF), lpClrData); Here I got the data very perfactly in buffer. Is there something missing in my code in Win32. Plz reply me. I will Appreciate your answer. Thanks in advance.

    Ashish Bhatt

    N 2 Replies Last reply
    0
    • A ashishbhatt 0

      Hi All,, I am getting problem while getting bitmap bits from bitmap in Win32 app.And I got my bitmap data using the same thing in MFC in another app. I used below code in Win32...... int iWidth = rect.right - rect.left; int iHeight = rect.bottom - rect.top; HBITMAP new_Bitmap = CreateCompatibleBitmap(m_hrootdc,iWidth,iHeight); HBITMAP temp_Bitmap = (HBITMAP)SelectObject(m_hmemdc,new_Bitmap); // Capture screen into bitmap BOOL blitok = BitBlt( m_hmemdc, rect.left - m_bmrect.left , rect.top - m_bmrect.top , rect.right - rect.left, rect.bottom - rect.top, m_hrootdc, rect.left, rect.top, SRCCOPY); int datasize = iWidth*iHeight; COLORREF* lpClrData = (COLORREF*)LocalAlloc(LPTR, datasize * sizeof(COLORREF)); datasize = GetBitmapBits(temp_Bitmap,datasize * sizeof(COLORREF), lpClrData); Here rect is the some rectangle portion,here I got datasize as some no of bytes but in lpClrData buffer I don,t get anythig. And now I am using the same thing in MFC like bellow..... HDC memDC = CreateCompatibleDC(m_hrootdc); CDC* pScreenDC = CDC::FromHandle(m_hrootdc); CDC* pMemDC = CDC::FromHandle(memDC); int iWidth = rect.right - rect.left; int iHeight = rect.bottom - rect.top; CBitmap newBitmap; if (!newBitmap.CreateCompatibleBitmap(pScreenDC, iWidth,iHeight)) { MessageBox(NULL,CString("CreateCompatibleBitmap failed."),CString("Test"),0); return; } CBitmap* pOldBitmap = pMemDC->SelectObject(&newBitmap); pMemDC->BitBlt(0, 0, iWidth,iHeight, pScreenDC, 0, 0, SRCCOPY); CBitmap* pbitmap = pMemDC->GetCurrentBitmap(); int datasize = iWidth*iHeight; COLORREF* lpClrData = (COLORREF*)LocalAlloc(LPTR, datasize * sizeof(COLORREF)); datasize = pbitmap->GetBitmapBits(datasize * sizeof(COLORREF), lpClrData); Here I got the data very perfactly in buffer. Is there something missing in my code in Win32. Plz reply me. I will Appreciate your answer. Thanks in advance.

      Ashish Bhatt

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      From MSDN: "This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the GetDIBits function."

      nave [OpenedFileFinder]

      A 2 Replies Last reply
      0
      • N Naveen

        From MSDN: "This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the GetDIBits function."

        nave [OpenedFileFinder]

        A Offline
        A Offline
        ashishbhatt 0
        wrote on last edited by
        #3

        But I am doing the same thing in MFC application as I have shown and i got the result then what is the problem here.

        Ashish Bhatt

        N 1 Reply Last reply
        0
        • N Naveen

          From MSDN: "This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the GetDIBits function."

          nave [OpenedFileFinder]

          A Offline
          A Offline
          ashishbhatt 0
          wrote on last edited by
          #4

          Ok,So as you told to try GetDIBits(), I used it but I don't get the result. I modified the original code as below..... BYTE* lpClrData = (BYTE*)LocalAlloc(LPTR, datasize * sizeof(BYTE)); if(GetDIBits(m_hmemdc,new_Bitmap,rect.right - rect.left, rect.bottom - rect.top,lpClrData,&m_bminfo.bmi,DIB_RGB_COLORS) == 0) { MessageBox(NULL,CString("Failed to get Bits from DIB"),CString("TestRTMP"),0); } Above function always return zero.So I don't get the ouotput. Is there any other thing which I am missing in this code plz suggest me. Thanks.

          Ashish Bhatt

          1 Reply Last reply
          0
          • A ashishbhatt 0

            But I am doing the same thing in MFC application as I have shown and i got the result then what is the problem here.

            Ashish Bhatt

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #5

            ashishbhatt wrote:

            // Capture screen into bitmap BOOL blitok = BitBlt( m_hmemdc, rect.left - m_bmrect.left , rect.top - m_bmrect.top , rect.right - rect.left, rect.bottom - rect.top, m_hrootdc, rect.left, rect.top, SRCCOPY);

            Every thing seems to be fine. But can you tell me or verify what will be position values passed to BitBlt() function like rect.left - m_bmrect.left , rect.top - m_bmrect.top etc.

            nave [OpenedFileFinder]

            A 1 Reply Last reply
            0
            • N Naveen

              ashishbhatt wrote:

              // Capture screen into bitmap BOOL blitok = BitBlt( m_hmemdc, rect.left - m_bmrect.left , rect.top - m_bmrect.top , rect.right - rect.left, rect.bottom - rect.top, m_hrootdc, rect.left, rect.top, SRCCOPY);

              Every thing seems to be fine. But can you tell me or verify what will be position values passed to BitBlt() function like rect.left - m_bmrect.left , rect.top - m_bmrect.top etc.

              nave [OpenedFileFinder]

              A Offline
              A Offline
              ashishbhatt 0
              wrote on last edited by
              #6

              Ya thanks. here everything comes right somthing like 0,0 position and 1280,1024 position. I hope problem is not in these value. Plz check the compatibleBitmap() and also SelectObject() function and ma I passing right bitmap handle or not.

              Ashish Bhatt

              1 Reply Last reply
              0
              • A ashishbhatt 0

                Hi All,, I am getting problem while getting bitmap bits from bitmap in Win32 app.And I got my bitmap data using the same thing in MFC in another app. I used below code in Win32...... int iWidth = rect.right - rect.left; int iHeight = rect.bottom - rect.top; HBITMAP new_Bitmap = CreateCompatibleBitmap(m_hrootdc,iWidth,iHeight); HBITMAP temp_Bitmap = (HBITMAP)SelectObject(m_hmemdc,new_Bitmap); // Capture screen into bitmap BOOL blitok = BitBlt( m_hmemdc, rect.left - m_bmrect.left , rect.top - m_bmrect.top , rect.right - rect.left, rect.bottom - rect.top, m_hrootdc, rect.left, rect.top, SRCCOPY); int datasize = iWidth*iHeight; COLORREF* lpClrData = (COLORREF*)LocalAlloc(LPTR, datasize * sizeof(COLORREF)); datasize = GetBitmapBits(temp_Bitmap,datasize * sizeof(COLORREF), lpClrData); Here rect is the some rectangle portion,here I got datasize as some no of bytes but in lpClrData buffer I don,t get anythig. And now I am using the same thing in MFC like bellow..... HDC memDC = CreateCompatibleDC(m_hrootdc); CDC* pScreenDC = CDC::FromHandle(m_hrootdc); CDC* pMemDC = CDC::FromHandle(memDC); int iWidth = rect.right - rect.left; int iHeight = rect.bottom - rect.top; CBitmap newBitmap; if (!newBitmap.CreateCompatibleBitmap(pScreenDC, iWidth,iHeight)) { MessageBox(NULL,CString("CreateCompatibleBitmap failed."),CString("Test"),0); return; } CBitmap* pOldBitmap = pMemDC->SelectObject(&newBitmap); pMemDC->BitBlt(0, 0, iWidth,iHeight, pScreenDC, 0, 0, SRCCOPY); CBitmap* pbitmap = pMemDC->GetCurrentBitmap(); int datasize = iWidth*iHeight; COLORREF* lpClrData = (COLORREF*)LocalAlloc(LPTR, datasize * sizeof(COLORREF)); datasize = pbitmap->GetBitmapBits(datasize * sizeof(COLORREF), lpClrData); Here I got the data very perfactly in buffer. Is there something missing in my code in Win32. Plz reply me. I will Appreciate your answer. Thanks in advance.

                Ashish Bhatt

                N Offline
                N Offline
                Naveen
                wrote on last edited by
                #7

                ashishbhatt wrote:

                datasize = GetBitmapBits(temp_Bitmap,datasize * sizeof(COLORREF), lpClrData);

                :) Here lies the problem you are calling GetBitmapBits() with temp_Bitmap instead of new_Bitmap .

                nave [OpenedFileFinder]

                A 1 Reply Last reply
                0
                • N Naveen

                  ashishbhatt wrote:

                  datasize = GetBitmapBits(temp_Bitmap,datasize * sizeof(COLORREF), lpClrData);

                  :) Here lies the problem you are calling GetBitmapBits() with temp_Bitmap instead of new_Bitmap .

                  nave [OpenedFileFinder]

                  A Offline
                  A Offline
                  ashishbhatt 0
                  wrote on last edited by
                  #8

                  Ya, Thanks. But Now I am getting little much strange problem.Let me explain little much more abt my application.In my application the function in which this bitmap capture is available calls very frequently with diff diff rectangle position. Now my problem is that at first rectangle data doesn't come(means I checked color value from the buffer it is black color for every pixel)but from second time it comes the data comes right. Every time data comes right aftere every one rectangle. I hope it very silly mistake but i am not getting it Here I write code from which I print the Pixel values using setpixel. unsigned char *destData = new unsigned char[iWidth*iHeight*sizeof(unsigned char *)] ; unsigned char *srcColor = new unsigned char[iWidth*iHeight*sizeof(unsigned char *)] ; int color = 0; for(int x = iHeight - 1; x >= 0; x--) { for(int y = 0; y < iWidth; y++) { srcColor[color++] = GetRValue(lpClrData[iWidth* x + y]); srcColor[color++] = GetGValue(lpClrData[iWidth* x + y]); srcColor[color++] = GetBValue(lpClrData[iWidth* x + y]); ::SetPixel(m_hrootdc,x ,y ,RGB(srcColor[color-3],srcColor[color-2],srcColor[color-1])); } }

                  Ashish Bhatt

                  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