A query in bitmap routine
-
Below is the code snippet I wrote for displaying pixel values onto the screen in MFC. The pixel values are stored in buffer disp_frame. But nothing appears on screen. Could anyone debug this code. void CBluetooth_decoderDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default int p=0,i=0,j=0,q=0; // extern int disp_frame[50][144][176]; /////////////////////// BITMAPINFOHEADER bmi; BITMAPINFO bm; RGBQUAD rgbarray[256]; bmi.biSize = sizeof(BITMAPINFOHEADER); bmi.biWidth = 176; bmi.biHeight = 144; bmi.biPlanes = 1; bmi.biBitCount = 8; bmi.biCompression = BI_RGB; bmi.biSizeImage = 0; bmi.biXPelsPerMeter = 0; bmi.biYPelsPerMeter = 0; bmi.biClrUsed = 0; bmi.biClrImportant = 0; bm.bmiHeader = bmi; for (int color_index = 0; color_index < 256; color_index++) { rgbarray[color_index].rgbBlue = i; rgbarray[color_index].rgbGreen = i; rgbarray[color_index].rgbRed = i; rgbarray[color_index].rgbReserved = 0; } bm.bmiColors[1] = rgbarray[0]; ///////////////////// CRect rcClient; GetClientRect(rcClient); // See Note 1 rcClient.right=176; rcClient.bottom=144; CDC MemDC,*pDC; CBitmap MemBitmap,*drawmem; pDC = this->GetDC(); // Get Current DC MemDC.CreateCompatibleDC(pDC); CreateDIBSection(MemDC,bm,DIB_RGB_COLORS,disp_frame,NULL,0); CBitmap *pOldBitmap =MemDC.SelectObject(&MemBitmap); pDC->BitBlt(150,100,176,144,&MemDC,0,0,SRCCOPY); MemDC.SelectObject(pOldBitmap); ReleaseDC(pDC); }
-
Below is the code snippet I wrote for displaying pixel values onto the screen in MFC. The pixel values are stored in buffer disp_frame. But nothing appears on screen. Could anyone debug this code. void CBluetooth_decoderDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default int p=0,i=0,j=0,q=0; // extern int disp_frame[50][144][176]; /////////////////////// BITMAPINFOHEADER bmi; BITMAPINFO bm; RGBQUAD rgbarray[256]; bmi.biSize = sizeof(BITMAPINFOHEADER); bmi.biWidth = 176; bmi.biHeight = 144; bmi.biPlanes = 1; bmi.biBitCount = 8; bmi.biCompression = BI_RGB; bmi.biSizeImage = 0; bmi.biXPelsPerMeter = 0; bmi.biYPelsPerMeter = 0; bmi.biClrUsed = 0; bmi.biClrImportant = 0; bm.bmiHeader = bmi; for (int color_index = 0; color_index < 256; color_index++) { rgbarray[color_index].rgbBlue = i; rgbarray[color_index].rgbGreen = i; rgbarray[color_index].rgbRed = i; rgbarray[color_index].rgbReserved = 0; } bm.bmiColors[1] = rgbarray[0]; ///////////////////// CRect rcClient; GetClientRect(rcClient); // See Note 1 rcClient.right=176; rcClient.bottom=144; CDC MemDC,*pDC; CBitmap MemBitmap,*drawmem; pDC = this->GetDC(); // Get Current DC MemDC.CreateCompatibleDC(pDC); CreateDIBSection(MemDC,bm,DIB_RGB_COLORS,disp_frame,NULL,0); CBitmap *pOldBitmap =MemDC.SelectObject(&MemBitmap); pDC->BitBlt(150,100,176,144,&MemDC,0,0,SRCCOPY); MemDC.SelectObject(pOldBitmap); ReleaseDC(pDC); }
Have a look at
CreateDIBSection
documentation [^]. It states the5th
parameter (a pointer to a pointer) is an[out]
one. It means you have to pass the address of a pointer and, on return, that pointer will contain the address of the allocated DIB section buffer, then you have to change the bit values of such buffer, according todisp_frame
values, before selecting it into device context. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Have a look at
CreateDIBSection
documentation [^]. It states the5th
parameter (a pointer to a pointer) is an[out]
one. It means you have to pass the address of a pointer and, on return, that pointer will contain the address of the allocated DIB section buffer, then you have to change the bit values of such buffer, according todisp_frame
values, before selecting it into device context. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkertant = 0; bm.bmiHeader = bmi; for (int color_index = 0; color_index < 256; color_index++) { rgbarray[color_index].rgbBlue = i; rgbarray[color_index].rgbGreen = i; rgbarray[color_index].rgbRed = i; rgbarray[color_index].rgbReserved = 0; } bm.bmiColors[1] = rgbarray[0]; ///////////////////// CRect rcClient; GetClientRect(rcClient); // See Note 1 rcClient.right=176; rcClient.bottom=144; CDC MemDC,*pDC; CBitmap MemBitmap,*drawmem; pDC = this->GetDC(); // Get Current DC MemDC.CreateCompatibleDC(pDC); CreateDIBSection(MemDC,bm,DIB_RGB_COLORS,disp_frame,NULL,0); CBitmap *pOldBitmap =MemDC.SelectObject(&MemBitmap); pDC->BitBlt(150,100,176,144,&MemDC,0,0,SRCCOPY); MemDC.SelectObject(pOldBitmap); ReleaseDC(pDC); } This was the piece of code which was posted previously. But the CreateDIBSection code CreateDIBSection(MemDC,bm,DIB_RGB_COLORS,disp_frame,NULL,0); was having MemDC is a CDC type. But actually it should be HDC type. So I have rewritten the code as HDC hDC; hDC = CreateDC("DISPLAY",NULL,NULL,NULL); HDC memDC = CreateCompatibleDC(hDC); CreateDIBSection(hDC,bitmap,DIB_PAL_COLORS,ppbits,NULL,0); HBITMAP memBM = CreateCompatibleBitmap(hDC,176,144); SelectObject(memDC,memBM); BitBlt(memDC,150,100,176,144,hDC,0,0,SRCCOPY); But CreateDIBSection is creating exception. What could be the reason. Basically I could not get the difference between CDC and HDC. Could anyone give a clear solution to my problem. I am actually trying to display a grayscale image which exists as R=G=B values in memory.
-
rtant = 0; bm.bmiHeader = bmi; for (int color_index = 0; color_index < 256; color_index++) { rgbarray[color_index].rgbBlue = i; rgbarray[color_index].rgbGreen = i; rgbarray[color_index].rgbRed = i; rgbarray[color_index].rgbReserved = 0; } bm.bmiColors[1] = rgbarray[0]; ///////////////////// CRect rcClient; GetClientRect(rcClient); // See Note 1 rcClient.right=176; rcClient.bottom=144; CDC MemDC,*pDC; CBitmap MemBitmap,*drawmem; pDC = this->GetDC(); // Get Current DC MemDC.CreateCompatibleDC(pDC); CreateDIBSection(MemDC,bm,DIB_RGB_COLORS,disp_frame,NULL,0); CBitmap *pOldBitmap =MemDC.SelectObject(&MemBitmap); pDC->BitBlt(150,100,176,144,&MemDC,0,0,SRCCOPY); MemDC.SelectObject(pOldBitmap); ReleaseDC(pDC); } This was the piece of code which was posted previously. But the CreateDIBSection code CreateDIBSection(MemDC,bm,DIB_RGB_COLORS,disp_frame,NULL,0); was having MemDC is a CDC type. But actually it should be HDC type. So I have rewritten the code as HDC hDC; hDC = CreateDC("DISPLAY",NULL,NULL,NULL); HDC memDC = CreateCompatibleDC(hDC); CreateDIBSection(hDC,bitmap,DIB_PAL_COLORS,ppbits,NULL,0); HBITMAP memBM = CreateCompatibleBitmap(hDC,176,144); SelectObject(memDC,memBM); BitBlt(memDC,150,100,176,144,hDC,0,0,SRCCOPY); But CreateDIBSection is creating exception. What could be the reason. Basically I could not get the difference between CDC and HDC. Could anyone give a clear solution to my problem. I am actually trying to display a grayscale image which exists as R=G=B values in memory.
Please surround code snippets with
<pre>
tags. (1) What error message are you receiving? (2) how did you defineppbits
?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Please surround code snippets with
<pre>
tags. (1) What error message are you receiving? (2) how did you defineppbits
?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeThe error message was Unhandled exception in decoder.exe(GDI32.dll): 0xc0000005 : Access Violation the declaration for ppbits was void **ppbits. Once this CreateDIBSection works i thought of filling ppbits with pixel values. That's why that particular piece of code is not available
-
The error message was Unhandled exception in decoder.exe(GDI32.dll): 0xc0000005 : Access Violation the declaration for ppbits was void **ppbits. Once this CreateDIBSection works i thought of filling ppbits with pixel values. That's why that particular piece of code is not available
jossion wrote:
the declaration for ppbits was void **ppbits
The above is wrong. You have to declare a pointer to void, for instance
void * pbits;
and then pass the address of the pointer (i.e.
&pbits
) toCreateDIBSection
:CreateDIBSection(hDC,bitmap,DIB_PAL_COLORS, &pbits, NULL,0);
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke