Problem with GetBitmapBits() in Win32
-
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
-
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
From MSDN: "This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the GetDIBits function."
nave [OpenedFileFinder]
-
From MSDN: "This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the GetDIBits function."
nave [OpenedFileFinder]
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
-
From MSDN: "This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the GetDIBits function."
nave [OpenedFileFinder]
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
-
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
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]
-
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]
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
-
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
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]
-
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]
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