Wrong display depending on displayed bitmap size - mfc

I am having a trouble in a SDI view.
I am using the following code to display a bitmap buffer.
Depending on the width of the bitmap and when the width is becoming large, then a flickering problem occurs.
Additionally, the view is displaying weird data as seen below :
Here is the expected display.
The code I am using for this view is here :
void CTestLargeView::RefreshDisplay()
{
CClientDC dc(this);
CRect cRect;
GetClientRect(&cRect);
LPBITMAPINFO pBmpInfo;
pBmpInfo = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pBmpInfo->bmiHeader.biWidth = XSize;
pBmpInfo->bmiHeader.biHeight = YSize;
pBmpInfo->bmiHeader.biPlanes = 1;
pBmpInfo->bmiHeader.biBitCount = 32;
pBmpInfo->bmiHeader.biCompression = BI_RGB;
pBmpInfo->bmiHeader.biSizeImage = XSize * YSize;
pBmpInfo->bmiHeader.biXPelsPerMeter = 0;
pBmpInfo->bmiHeader.biYPelsPerMeter = 0;
pBmpInfo->bmiHeader.biClrUsed = 0;
pBmpInfo->bmiHeader.biClrImportant = 0;
SetStretchBltMode(dc.m_hDC, STRETCH_DELETESCANS);
StretchDIBits(dc.m_hDC,
0,
0,
cRect.Width(),
cRect.Height(),
0,
0,
XSize,
YSize,
Data,
pBmpInfo,
DIB_RGB_COLORS,
SRCCOPY);
delete[] pBmpInfo;
}
void CTestLargeView::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 150)
RefreshDisplay();
CView::OnTimer(nIDEvent);
}
int CTestLargeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
SetTimer(150, 33, NULL);
XSize = 32000; // No flickering
//XSize = 32800; // Flickering occurring
YSize = 256;
Data = new int[XSize * YSize];
for (int i = 0 ; i < XSize * YSize ; i++)
Data[i] = RGB(i % 255, i % 255, i % 255);
return 0;
}
Thanks !

Found the problem->
It is caused by the "STRETCH_DELETESCANS". Changing it to STRETCH_HALFTONE is correcting the trouble.

Related

MFC dialog display 8 bit buffer

Added a picture control to my MFC dialog. Set its type to Frame. Need to display 8 bit buffer (NOT LOADED From file) on the picture control. The image is scaled down, repeated , tilted diagonally. Not correct. I could not make it fit the control properly. resized the control but still the same problem.
w = 750;
h = 850;
bpp = 8
void CDispDlg::BuildINfoAndDisp(int w, int h)
{
m_pbmi = (BITMAPINFO*) new BYTE[sizeof(BITMAPINFO) + 256) * sizeof(RGBQUAD)];
m_pbmi->bmiHeader.bSize=sizeof(BITMAPINFOHEADER);
m_pbmi->bmiHeader.biPlanes = 1;
m_pbmi->bmiHeader.biBitCount = bpp;
m_pbmi->bmiHeader.biCompression = BI_RGB;
m_pbmi->bmiHeader.biSizeImage = 0;
m_pbmi->bmiHeader.biXpelsPerMeter = 0;
m_pbmi->bmiHeader.biYpelsPerMeter = 0;
m_pbmi->bmiHeader.biClrUsed = 0;
m_pbmi->bmiHeader.biClrImportant = 0;
for (int i = 0; i< 256; i++)
{
m_pbmi->bmiColors[i].rgbBlue = i;
m_pbmi->bmiColors[i].rgbGreen = i;
m_pbmi->bmiColors[i].rgbBRed = i;
m_pbmi->bmiColors[i].rgbReserved = 0;
}
m_pbmi->bmiHeader.biWidth = w;
m_pbmi->bmiHeader.biHeight = -h;
CClientDC dc(GetDlgItem(IDC_PicCntrl_DSIP));
CRect rect;
GetDlgItem(IDC_PicCntrl_DSIP)->GetClientRect(&rect);
SetStrechBltMode(dc.GetSafeHdc(), COLORNOCOLOR);
StretchBIBits(dc.GetSafeHdc(), 0 ,0 , rect.Width, rect.Height, 0,0, m_wImage, m_hImage, &m_U8imageBuf, m_pbmi,DIB_RGB_COLORS, SRCCOPY);
}

Windows GetDIBits not returning expected values

I'm currently writing a small program that scans the screen and looks for pixels. My issue is that GetDIBits function doesn't appear to return a proper screenshot of the screen.
Copying the bitmap to the clipboard does put the right screen image in the clipboard.
I decided to print out the function's output to a BMP file to get an idea of what's going on and it clearly isn't what I'm expecting.
I'll also mention that I have 3 monitors, incase that could explain why it's not behaving like expected.
class Test {
int screenWidth;
int screenHeight;
HWND targetWindow;
HDC targetDC;
HDC captureDC;
RGBQUAD *pixels;
HBITMAP captureBitmap;
bool TakeScreenshot() {
ZeroMemory(pixels, screenHeight*screenWidth);
screenWidth = GetSystemMetrics(SM_CXSCREEN);
screenHeight = GetSystemMetrics(SM_CYSCREEN);
targetWindow = GetDesktopWindow();
targetDC = GetDC(NULL);
captureDC = CreateCompatibleDC(targetDC);
captureBitmap = CreateCompatibleBitmap(targetDC, screenWidth, screenHeight);
HGDIOBJ old = SelectObject(captureDC, captureBitmap);
if (!old)
printf("Error selecting object\n");
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, captureBitmap);
CloseClipboard();
if (BitBlt(captureDC, 0, 0, screenWidth, screenHeight, targetDC, 0, 0, SRCCOPY)) {
BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = screenWidth;
bmi.bmiHeader.biHeight = -screenHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
if (!SelectObject(captureDC, old))
printf("Error unselecting object\n");
if (!GetDIBits(captureDC,
captureBitmap,
0,
screenHeight,
pixels,
&bmi,
DIB_RGB_COLORS
)) {
printf("%s: GetDIBits failed\n", __FUNCTION__);
return false;
}
}
else {
printf("%s: BitBlt failed\n", __FUNCTION__);
return false;
}
return true;
}
// This is from somewhere on stackoverflow - can't find where.
void MakePicture() {
typedef struct /**** BMP file header structure ****/
{
unsigned int bfSize; /* Size of file */
unsigned short bfReserved1; /* Reserved */
unsigned short bfReserved2; /* ... */
unsigned int bfOffBits; /* Offset to bitmap data */
} BITMAPFILEHEADER;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
unsigned short bfType = 0x4d42;
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfSize = 2 + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 2560 * 1440 * 3;
bfh.bfOffBits = 0x36;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = screenWidth;
bih.biHeight = screenHeight;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = 0;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 5000;
bih.biYPelsPerMeter = 5000;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
FILE *file;
fopen_s(&file, "test.bmp", "wb");
if (!file)
{
printf("Could not write file\n");
return;
}
/*Write headers*/
fwrite(&bfType, 1, sizeof(bfType), file);
fwrite(&bfh, 1, sizeof(bfh), file);
fwrite(&bih, 1, sizeof(bih), file);
/*Write bitmap*/
for (int y = 0; y < screenHeight; y++)
{
for (int x = 0; x < screenWidth; x++)
{
unsigned char r = pixels[x + y].rgbRed;
unsigned char g = pixels[x + y].rgbGreen;
unsigned char b = pixels[x + y].rgbBlue;
fwrite(&b, 1, 1, file);
fwrite(&g, 1, 1, file);
fwrite(&r, 1, 1, file);
}
}
fclose(file);
}
Test() {
screenWidth = GetSystemMetrics(SM_CXSCREEN);
screenHeight = GetSystemMetrics(SM_CYSCREEN);
pixels = new RGBQUAD[screenWidth * screenHeight];
}
~Test() {
//cleanup
}
};
Here is the result that the code is giving(instead of a screenshot):
It appears like it takes a few pixels from the top of my screen and stretches them into an image. The screenshot is from Visual Studio being open(orange part being the notifications).
If I put a giant red square (255, 0, 0) in my screen, if it's height isn't 0, the pixels array will not contain a single red pixel.
BitBlt performs the actual copying. Clipboard functions should be called after BitBlt
Also note, in mutli-monitor settings, SM_CXSCREEN/Y... give the size for the primary monitor. Use SM_XVIRTUALSCREEN/XV... for the whole screen. SM_XVIRTUALSCREEN/Y will give the X/Y coordinate (it's usually zero)
Be sure to release all the handles and delete the used objects when you are finished. In fact, there is no need to declare targetDC etc. as class members.
If the application is not DPI aware, the bitmap may look smaller depending on DPI settings. Call SetProcessDPIAware() at the start of the program for a quick fix, or set the manifest.
As noted in comment, with SetClipboardData(CF_BITMAP, captureBitmap); the system takes over captureBitmap. Avoid calling this function or make a copy of the bitmap to pass to clipboard.
int main()
{
int screenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);
int screenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int screen_x = GetSystemMetrics(SM_XVIRTUALSCREEN);
int screen_y = GetSystemMetrics(SM_YVIRTUALSCREEN);
screenWidth = GetSystemMetrics(SM_CXSCREEN);
screenHeight = GetSystemMetrics(SM_CYSCREEN);
screen_x = 0;
screen_y = 0;
RGBQUAD* pixels = new RGBQUAD[screenWidth * screenHeight];
DWORD size = screenWidth * screenHeight * 4;
ZeroMemory(pixels, size);
HDC targetDC = GetDC(NULL);
HDC captureDC = CreateCompatibleDC(targetDC);
HBITMAP captureBitmap = CreateCompatibleBitmap(targetDC, screenWidth, screenHeight);
HGDIOBJ old = SelectObject(captureDC, captureBitmap);
if(!BitBlt(captureDC, 0, 0, screenWidth, screenHeight, targetDC,
screen_x, screen_y, SRCCOPY))
printf("BitBlt error\n");
SelectObject(captureDC, old);
BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = screenWidth;
bmi.bmiHeader.biHeight = -screenHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
if(OpenClipboard(NULL))
{
EmptyClipboard();
SetClipboardData(CF_BITMAP,
CopyImage(captureBitmap, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE));
CloseClipboard();
}
if(!GetDIBits(targetDC,
captureBitmap,
0,
screenHeight,
pixels,
&bmi,
DIB_RGB_COLORS
))
printf("%s: GetDIBits failed\n", __FUNCTION__);
BITMAPFILEHEADER filehdr = { 'MB', 54 + size, 0, 0, 54 };
std::ofstream f("test.bmp", std::ios::binary);
f.write((char*)&filehdr, sizeof(filehdr));
f.write((char*)&bmi, sizeof(bmi));
f.write((char*)pixels, size);
//cleanup:
SelectObject(captureDC, old);
DeleteObject(captureBitmap);
DeleteDC(captureDC);
ReleaseDC(0, targetDC);
}
GetDIBits function reference, remarks section:
The bitmap identified by the hbmp parameter must not be selected into
a device context when the application calls this function.
Deselect bitmap before calling GetBIBits.
HBITMAP oldBitmap = SelectObject(captureDC, captureBitmap);
...
// Deselect captureBitmap by selecting oldBitmap.
SelectObject(captureDC, oldBitmap);
Remember to add cleanup code (restore bitmap, destroy bitmap, destroy or release device contexts).
Additional bug:
for (int y = 0; y < screenHeight; y++)
{
for (int x = 0; x < screenWidth; x++)
{
unsigned char r = pixels[x + y].rgbRed;
unsigned char g = pixels[x + y].rgbGreen;
unsigned char b = pixels[x + y].rgbBlue;
fwrite(&b, 1, 1, file);
fwrite(&g, 1, 1, file);
fwrite(&r, 1, 1, file);
}
}
I think it should be
for (int y = 0; y < screenHeight; y++)
{
for (int x = 0; x < screenWidth; x++)
{
unsigned char r = pixels[x + y*screenWidth].rgbRed;
unsigned char g = pixels[x + y*screenWidth].rgbGreen;
unsigned char b = pixels[x + y*screenWidth].rgbBlue;
fwrite(&b, 1, 1, file);
fwrite(&g, 1, 1, file);
fwrite(&r, 1, 1, file);
}
}
But rows require padding to multiplies of 4 bytes:
// Important: each row has to be padded to multiple of DWORD.
// Valid only for 24 bits per pixel bitmaps.
// Remark: 32 bits per pixel have rows always aligned (padding==0)
int padding = 3 - (screenWidth*3 + 3)%4;
// or
// int padding = 3 - ((screenWidth*3 + 3) & 3);
for (int y = 0; y < screenHeight; y++)
{
for (int x = 0; x < screenWidth; x++)
{
unsigned char r = pixels[x + y*screenWidth].rgbRed;
unsigned char g = pixels[x + y*screenWidth].rgbGreen;
unsigned char b = pixels[x + y*screenWidth].rgbBlue;
fwrite(&b, 1, 1, file);
fwrite(&g, 1, 1, file);
fwrite(&r, 1, 1, file);
}
// Important: each row has to be padded to multiple of DWORD.
fwrite("\0\0\0\0", 1, padding, file);
}
Adjust file size (valid for 24 bits per pixel):
bfh.bfSize =
2
+ sizeof(BITMAPFILEHEADER)
+ sizeof(BITMAPINFOHEADER)
+ ((screenWidth*3 + 3) & ~3) * screenHeight;

C++ get average colour of multible squares on screen (ambilight)

Im new and i hope you can help me.
First some infos:
I wanna create a ambilight for my Keyboard(Roccat MK FX)
My Screen is 2560x1440.
I wanna split the screen in multible squares. For each Key on the board 1 square.
The board layout: enter image description here
The Splitting of the screen(more or less):
enter image description here
SOOO i wanna get the average colour of field "esc" and put it in the key "0" and so on.
I only have problems to get the colour of the screen.
I wanna only read every 10 pixel for speed and so on. maybe i lower it or so. i will see it later^^
the code is only for the top row of keys.
every square for a key has the dimension of 160x240 pixels for all 16 keys.
HDC hScreen = GetDC(0);
ScreenX = GetDeviceCaps(hScreen, HORZRES);
ScreenY = GetDeviceCaps(hScreen, VERTRES);
HDC hdcMem = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, ScreenX, ScreenY);
HGDIOBJ hOld = SelectObject(hdcMem, hBitmap);
BitBlt(hdcMem, 0, 0, ScreenX, ScreenY, hScreen, 0, 0, SRCCOPY);
SelectObject(hdcMem, hOld);
BITMAPINFOHEADER bmi = { 0 };
bmi.biSize = sizeof(BITMAPINFOHEADER);
bmi.biPlanes = 1;
bmi.biBitCount = 32;
bmi.biWidth = ScreenX;
bmi.biHeight = -ScreenY;
bmi.biCompression = BI_RGB;
bmi.biSizeImage = 0;// 3 * ScreenX * ScreenY;
if (ScreenData)
free(ScreenData);
ScreenData = (BYTE*)malloc(4*ScreenX * ScreenY);
GetDIBits(hdcMem, hBitmap, 0, ScreenY, ScreenData, (BITMAPINFO*)&bmi, DIB_RGB_COLORS);
ReleaseDC(0, hScreen);
DeleteDC(hdcMem);
DeleteObject(hBitmap);
if (tasta)
{
//teiler = 1;
redcolor = 0;
greencolor = 0;
bluecolor = 0;
redtemp = 0;
greentemp = 0;
bluetemp = 0;
hori = 0;
vert = 0;
memset(frame_dataOnOff, 0, 110);
memset(frame_dataRed, 0, 110);
memset(frame_dataGreen, 0, 110);
memset(frame_dataBlue, 0, 110);
x_cord = 0;
for (int k = 0; k <= 15; k++)//eigne einteilung wählen
{
redcolor = 0;
greencolor = 0;
bluecolor = 0;
teiler = 0;
for (int y = 1440; y > 1200; y = y - 10)//VERTIKAL 1440 240
{
for (int x = 160 * k; x <= 160 * k + 160; x = x + 10)//Horizontal 2560 variable
{
redcolor = redcolor + ScreenData[4 * (((y)*ScreenX) + x) + 2];
greencolor = greencolor + ScreenData[4 * (((y)*ScreenX) + x) + 1];
bluecolor = bluecolor + ScreenData[4 * (((y)*ScreenX) + x)];
teiler++;
}
}
redcolor = redcolor / teiler;
greencolor = greencolor / teiler;
bluecolor = bluecolor / teiler;
frame_dataOnOff[k] = 1;
frame_dataRed[k] = redcolor;
frame_dataGreen[k] = greencolor;
frame_dataBlue[k] = bluecolor;
}
x_cord = 0;
ReleaseDC(0, hScreen);
DeleteDC(hdcMem);
DeleteObject(hBitmap);
}
with this code i get a error when k is 6 and x is 1010 error is: Ausgelöste Ausnahme: Lesezugriffsverletzung "ScreenData" war "0x62C511C". ScreenData for red = 14749642
ScreenData for blue= 14749641
ScreenData for green = 14749640
I think im out of range or so. but dont know
Thx for help ;)
ps. when i change the multiplier from ScreenData from 4 to 3, the code works, but the colour squares ae on the wrong place. more in the left middle of the screen.

MFC casting Handle into pointer and DIB to DDB conversion

I am trying to create a bitmap by hardcoding an array of pixel values, converting this array of pixels into a DIB, and then turn this DIB into a DDB. I found two functions to convert CreateBitmapFromPixels and DIBToDDB on the internet. My problem is that the program would crash at line 244. I found that, at line 243, lpbi does not retrieve information from hDIB. Then I added the code at lines 229 and 230 to see if doing the same thing in the function that created the BITMAPINFO structure would help. Still, nothing was gotten from the HBITMAP. I am wondering if there is anything wrong with casting a handle into a pointer, what does it do, and are there other ways to get the HBITMAPINFOHEADER from a handle to a DIB so I can fix the problem.
HBITMAP ColorChange2Dlg::CreateBitmapFromPixels( HDC hDC,
UINT uWidth, UINT uHeight, UINT uBitsPerPixel, LPVOID pBits)
{
if(uBitsPerPixel < 8) // NOT IMPLEMENTED YET
return NULL;
if(uBitsPerPixel == 8)
return Create8bppBitmap(hDC, uWidth, uHeight, pBits);
HBITMAP hBitmap = 0;
if ( !uWidth || !uHeight || !uBitsPerPixel )
return hBitmap;
LONG lBmpSize = uWidth * uHeight * (uBitsPerPixel/8) ;
BITMAPINFO bmpInfo = { 0 };
bmpInfo.bmiHeader.biBitCount = uBitsPerPixel;
bmpInfo.bmiHeader.biHeight = uHeight;
bmpInfo.bmiHeader.biWidth = uWidth;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
if(bmpInfo.bmiHeader.biBitCount==32) {
bmpInfo.bmiHeader.biCompression=BI_RGB;
//bmpInfo.bmiColors=NULL;
}
// Pointer to access the pixels of bitmap
UINT * pPixels = 0;
hBitmap = CreateDIBSection( hDC, (BITMAPINFO *)&
bmpInfo, DIB_RGB_COLORS, (void **)&
pPixels , NULL, 0);
if ( !hBitmap )
return hBitmap; // return if invalid bitmaps
//SetBitmapBits( hBitmap, lBmpSize, pBits);
// Directly Write
memcpy(pPixels, pBits, lBmpSize );
LPBITMAPINFOHEADER lpbi; //Line 229
lpbi = (LPBITMAPINFOHEADER)hBitmap; //Line 230
return hBitmap;
}
HBITMAP ColorChange2Dlg::DIBToDDB( HANDLE hDIB, CDC& dc )
{
LPBITMAPINFOHEADER lpbi;
HBITMAP hbm;
CPalette pal;
CPalette* pOldPal;
//CClientDC dc(NULL);
if (hDIB == NULL)
return NULL;
lpbi = (LPBITMAPINFOHEADER)hDIB; //Line 243
int nColors = lpbi->biClrUsed ? lpbi->biClrUsed : 1 << lpbi->biBitCount; //Line 244
BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
LPVOID lpDIBBits;
if( bmInfo.bmiHeader.biBitCount > 8 )
lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
bmInfo.bmiHeader.biClrUsed) +
((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
else
lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
// Create and select a logical palette if needed
if( nColors <= 256 && dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
{
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;
pLP->palNumEntries = nColors;
for( int i=0; i < nColors; i++)
{
pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
pLP->palPalEntry[i].peFlags = 0;
}
pal.CreatePalette( pLP );
delete[] pLP;
// Select and realize the palette
pOldPal = dc.SelectPalette( &pal, FALSE );
dc.RealizePalette();
}
hbm = CreateDIBitmap(dc.GetSafeHdc(), // handle to device context
(LPBITMAPINFOHEADER)lpbi, // pointer to bitmap info header
(LONG)CBM_INIT, // initialization flag
lpDIBBits, // pointer to initialization data
(LPBITMAPINFO)lpbi, // pointer to bitmap info
DIB_RGB_COLORS ); // color-data usage
if (pal.GetSafeHandle())
dc.SelectPalette(pOldPal,FALSE);
return hbm;
}
void ColorChange2Dlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CClientDC dc(this);
COLORREF *pix = (COLORREF *)malloc(255*255*sizeof(COLORREF));
//int x = 1;
if(pix!=NULL){
for(int i=0;i<255;i++)
{
for(int j=0;j<255;j++)
{
pix[i*255+j] = RGB(i,j,0);
}
}
}
CDC tempDC;
tempDC.CreateCompatibleDC(&dc);
HBITMAP dib = CreateBitmapFromPixels(tempDC.m_hDC,255,255,8*sizeof(COLORREF),(BYTE*)pix);
HBITMAP finalMap = DIBToDDB(dib,tempDC);
HBITMAP oldMap = (HBITMAP)tempDC.SelectObject(finalMap);
dc.BitBlt(201,50,255,255,&tempDC,0,0,SRCCOPY);
tempDC.SelectObject(oldMap);
tempDC.DeleteDC();
}
To write compatible code, it's better not to access bits directly at all. You can use Gradient functions and GDI or GDI+ draw functions to do anything you want.
The code you have in mind pix[i*255+j] = RGB(i,j,0); is of a 32-bit image. Each pixel points to a color. It's not a palette image where each pixel points to an entry in the color table.
If display is 32 bit (most modern computers are, but check to make sure), you can do this with the following code
CBitmap m_bitmap;
void CMyWnd::make_bitmap()
{
if (m_bitmap.GetSafeHandle()) return;
int w = 255;
int h = 255;
int *pix = new int[w*h];
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++)
pix[i + j*w] = RGB(i, j, 0);
m_bitmap.CreateBitmap(w, h, 1, 32, pix);
delete[]pix;
}
And to draw the bitmap:
void CMyWnd::paint_bitmap(CDC &dc)
{
if (!m_bitmap.GetSafeHandle()) return;
CDC memdc;
memdc.CreateCompatibleDC(&dc);
HBITMAP oldbitmap = (HBITMAP)memdc.SelectObject(m_bitmap);
BITMAP bm;
m_bitmap.GetBitmap(&bm);
dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &memdc, 0, 0, SRCCOPY);
memdc.SelectObject(oldbitmap);
}
void CMyWnd::OnPaint()
{
__super::OnPaint();
CClientDC dc(this);
paint_bitmap(dc);
}
Edit: For historical reasons the RGB value are saved backward as BGR. Use this function instead:
void CMyWnd::make_bitmap()
{
if (m_bitmap.GetSafeHandle()) return;
int w = 256;
int h = 256;
BYTE *pix = new BYTE[4*w*h];
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
int p = (i + j*w) * 4;
pix[p + 0] = 0;//blue
pix[p + 1] = i;//green
pix[p + 2] = j;//red
pix[p + 3] = 0;//not used in GDI functions
}
}
m_bitmap.CreateBitmap(w, h, 1, 32, pix);
delete[]pix;
}

Creating a DIB from a buffer and blitting it to the screen

I'm trying to create my own image buffer filled with red pixels and then use that with a DIB to display it on a screen DC. The result is that I'm getting a dialog full of lines, almost like what would happen if the stride was off... maybe someone here can point out my mistake. The lines in the image are difficult to see, but they alternate as a line of red, then green, then blue.. repeating until the bottom of the dialog. Much appreciated if someone can help me out on this!
//Filling the buffer with all red pixels
uint8_t *buffer = new uint8_t[IMAGEX * IMAGEY * 3];
for (int y = 0; y < IMAGEY; y++)
{
for (int x = 0; x < IMAGEX * 3; x += 3)
{
buffer[y*IMAGEX*3 + x] = 0;
buffer[y*IMAGEX*3 + x + 1] = 0;
buffer[y*IMAGEX*3 + x + 2] = 255;
}
}
pCam->pTestQ->push_back(buffer);
//Displaying the buffer on a DC
BITMAPINFOHEADER bmih = {0};
bmih.biSize = sizeof(BITMAPINFOHEADER);
bmih.biWidth = IMAGEX;
bmih.biHeight = -IMAGEY;
bmih.biPlanes = 1;
bmih.biBitCount = 24;
bmih.biCompression = BI_RGB;
bmih.biXPelsPerMeter = 0;
bmih.biYPelsPerMeter = 0;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
bmih.biSizeImage = IMAGEX * IMAGEY * 3;
BITMAPINFO bmi = {0};
bmi.bmiHeader = bmih;
pDC = GetDC();
mDC.CreateCompatibleDC(pDC);
CBitmap img;
img.CreateCompatibleBitmap(pDC, IMAGEX, IMAGEY);
CBitmap* pOldBmp = mDC.SelectObject(&img);
int iRet = SetDIBits(mDC, img, 0, IMAGEY, pCamera->pTestQ->front(), &bmi, DIB_RGB_COLORS);
pDC->BitBlt(0, 0, IMAGEX, IMAGEY, &mDC, 0, 0, SRCCOPY);
mDC.SelectObject(pOldBmp);
ReleaseDC(pDC);
Resulting dc blit:
http://i.imgur.com/qtImSFE.png