how to flip a HBITMAP horizontally?As an option, I thought to get an array of colors from BITMAP and write them to another BITMAP, but somehow it's too busy. Are there built-in functions or other options to do this?
You can use StretchBlt with a negative dimension as below:
HBITMAP FlipBitmapHorizontally(HBITMAP hbm) {
BITMAP bm;
GetObject(hbm, sizeof(BITMAP), &bm);
int wd = bm.bmWidth;
int hgt = bm.bmHeight;
HDC hdcScr = GetDC(NULL);
HDC hdcFlipped = CreateCompatibleDC(hdcScr);
HBITMAP hbmFlipped = CreateCompatibleBitmap(hdcScr, wd, hgt);
HGDIOBJ oldFlipped = SelectObject(hdcFlipped, hbmFlipped);
HDC hdcSrc = CreateCompatibleDC(hdcScr);
HGDIOBJ oldSrc = SelectObject(hdcSrc, hbm);
StretchBlt(hdcFlipped, wd, 0, -wd, hgt, hdcSrc, 0, 0, wd, hgt, SRCCOPY);
SelectObject(hdcSrc, oldSrc);
DeleteDC(hdcSrc);
SelectObject(hdcFlipped, oldFlipped);
DeleteDC(hdcFlipped);
ReleaseDC(NULL, hdcScr);
return hbmFlipped;
}
I'm creating a program using the Win32 API, and I need to create a new bitmap filled with one color and with given dimensions.
Here is my code:
m_hBitmap =( HBITMAP ) CreateCompatibleBitmap(hDC, iWidth, iHeight);
HDC hDCn = CreateCompatibleDC( hDC );
SelectObject( hDCn, m_hBitmap );
ExtFloodFill(hDCn, 0, 0, crColor, FLOODFILLSURFACE);
DeleteDC( hDCn );
The bitmap dimensions match, but the bitmap is always black regardless of the crColor parameter.
Use FillRect() instead of ExtFloodFill().
Also, you need to de-select the bitmap before you delete the DC, otherwise the original bitmap created and selected into the DC by CreateCompatibleDC() will be leaked.
Try this:
m_hBitmap = CreateCompatibleBitmap(hDC, iWidth, iHeight);
HDC hDCn = CreateCompatibleDC(hDC);
HBITMAP hOld = (HBITMAP) SelectObject(hDCn, m_hBitmap); // <-- SAVE OLD BITMAP!
//ExtFloodFill(hDCn, 0, 0, crColor, FLOODFILLSURFACE);
RECT r;
r.left = r.top = 0;
r.right = iWidth;
r.bottom = iHeight;
HBRUSH hBrush = CreateSolidBrush(crColor);
FillRect(hDCn, &r, hBrush);
DeleteObject(hBrush);
SelectObject(hDCn, hOld); // <-- RESTORE OLD BITMAP
DeleteDC(hDCn);
I've looked at multiple responses to similar questions from both this site and others, and while I feel like I've gotten closer, I just can't quite get it right. Still, this is probably a super-noobish question.
So I used to only call the WndProc case "WM_Paint" (via InvalidateRect) one every few minutes, so I didn't really notice the leak. Now I've added something that calls it about 5 times a second. In that second my memory usage jumps about 3800k. Yeah, that got noticed...
Here's the code:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
// Other cases omitted since we skip them due to "case WM_Paint:".
case WM_PAINT:
wndProc_Paint(hwnd);
break;
// Other cases omitted since we skip them due to "break;".
}
return 0;
}
void wndProc_Paint(HWND hwnd)
{
g_hbmBoard = ConvertIplImageToHBITMAP(targetBoardImg); //OpenCV command
BITMAP bm;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps); // <- Breakpoint here while monitoring mem usage shows this is what is adding ~772k per call which never gets released.
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmBoard);
GetObject(g_hbmBoard, sizeof(bm), &bm);
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmGreenLight);
GetObject(g_hbmGreenLight, sizeof(bm), &bm);
BitBlt(hdc, screenResW - 59, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmWorkingLight);
GetObject(g_hbmWorkingLight, sizeof(bm), &bm);
BitBlt(hdc, screenResW - 94, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmWorkingIndicator);
GetObject(g_hbmWorkingIndicator, sizeof(bm), &bm);
BitBlt(hdc, screenResW - 129, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
//DeleteObject(hbmOld);
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
//SelectObject(hdc, hbmOld);
//ReleaseDC(hwnd, hdc);
//DeleteDC(hdc);
//DeleteObject(hdc);
EndPaint(hwnd, &ps);
}
With the exception of the g_hbmWorkingIndicator segment and the commented out parts, this is what my WM_Paint looked like before it got called 5times/sec (even before I put it into it's own function - which was a separate issue).
The line "HDC hdc = BeginPaint(hwnd, &ps);" is where the memory gets added, and it never gets released. This happens every time we go through the function. So now for the stuff I tried in fixing the issue.
Reading up on similar issues, I assumed that I needed to Release or Delete the DC of hdc. I wasn't (still aren't - yes I've read the MSDN page) sure how SelectObject works with these, so I've tried it with and without ReleaseDC & DeleteDC (&both) for both hdc and hdcMem. I also tried DeleteObject for hdc & hdcMem. None of those had any affect.
DeleteObject(hbmOld) had an effect. It solved the issue. Except it's the wrong solution, because although I don't have the memory climbing out of control, I do have some of the visuals getting replaced with the wrong visuals. g_hbmGreenLight usually gets g_hbmBoard's graphic while g_hbmWorkingLight turns green (g_hbmGreenLight's graphic). Moving "DeleteObject(hbmOld);" to after EndPaint(~) or trying to use a "SelectObject" just changes which objects get replaced by which wrong graphic - also; returns the memory leak.
EDIT: For completeness's sake, I have included the code for ConvertIplImageToHBITMAP(IplImage* image) here. It is entirely possible that this is the culprit.
HBITMAP ConvertIplImageToHBITMAP(IplImage* pImage)
{
IplImage* image = (IplImage*)pImage;
bool imgConverted = false;
if(pImage->nChannels != 3)
{
IplImage* imageCh3 = cvCreateImage(cvGetSize(pImage), 8, 3);
if(pImage->nChannels==1){cvCvtColor(pImage, imageCh3, CV_GRAY2RGB);}
image = imageCh3;
imgConverted = true;
}
int bpp = image->nChannels * 8;
assert(image->width >= 0 && image->height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));
CvMat dst;
void* dst_ptr = 0;
HBITMAP hbmp = NULL;
unsigned char buffer[sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD)];
BITMAPINFO* bmi = (BITMAPINFO*)buffer;
BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
ZeroMemory(bmih, sizeof(BITMAPINFOHEADER));
bmih->biSize = sizeof(BITMAPINFOHEADER);
bmih->biWidth = image->width;
bmih->biHeight = image->origin ? abs(image->height) : -abs(image->height);
bmih->biPlanes = 1;
bmih->biBitCount = bpp;
bmih->biCompression = BI_RGB;
if (bpp == 8)
{
RGBQUAD* palette = bmi->bmiColors;
int i;
for (i = 0; i < 256; i++)
{
palette[i].rgbRed = palette[i].rgbGreen = palette[i].rgbBlue = (BYTE)i;
palette[i].rgbReserved = 0;
}
}
hbmp = CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, &dst_ptr, 0, 0);
cvInitMatHeader(&dst, image->height, image->width, CV_8UC3, dst_ptr, (image->width * image->nChannels + 3) & -4);
cvConvertImage(image, &dst, image->origin ? CV_CVTIMG_FLIP : 0);
if(imgConverted)
{cvReleaseImage(&image);}
return hbmp;
}
So, all that said: Help me StackExchange, you're my only hope! ;_;
You are losing the original HBITMAP that is returned by SelectObject(hdcMem, g_hbmBoard), so you are not restoring it correctly before calling DeleteDC(), and thus it gets leaked. You are overwriting the hbmOld variable every time you call SelectObject(), but you are not restoring the current hbmOld value back into dcMem before overwriting hbmOld again. When using SelectObject(), you MUST restore the original object when you are done making changes to the HDC.
Try this instead:
void wndProc_Paint(HWND hwnd)
{
g_hbmBoard = ConvertIplImageToHBITMAP(targetBoardImg); //OpenCV command
BITMAP bm;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = (HBITMAP) SelectObject(hdcMem, g_hbmBoard); // save the original HBITMAP
GetObject(g_hbmBoard, sizeof(bm), &bm);
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, g_hbmGreenLight); // returns g_hbmBoard, no need to save it to hbmpOld
GetObject(g_hbmGreenLight, sizeof(bm), &bm);
BitBlt(hdc, screenResW - 59, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, g_hbmWorkingLight); // returns g_hbmGreenLight, no need to save it to hbmpOld
GetObject(g_hbmWorkingLight, sizeof(bm), &bm);
BitBlt(hdc, screenResW - 94, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, g_hbmWorkingIndicator); // returns g_hbmWorkingLight, no need to save it to hbmpOld
GetObject(g_hbmWorkingIndicator, sizeof(bm), &bm);
BitBlt(hdc, screenResW - 129, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld); // restore the original HBITMAP
DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
// who owns g_hbmBoard? ConvertIplImageToHBITMAP() creates
// a new HBITMAP, so you need to free it with DeleteObject()
// before calling ConvertIplImageToHBITMAP() again. It would
// be better to create g_hbmBoard one time outside of WM_PAINT,
// recreate g_hbmBoard only when the source image actually changes,
// and then re-use g_hbmBoard as-is inside of WM_PAINT.
// ConvertIplImageToHBITMAP() really does not belong in WM_PAINT...
//
//DeleteObject(g_hbmBoard);
}
Alternatively, use SaveDC()/RestoreDC() instead:
void wndProc_Paint(HWND hwnd)
{
g_hbmBoard = ConvertIplImageToHBITMAP(targetBoardImg); //OpenCV command
BITMAP bm;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HDC hdcMem = CreateCompatibleDC(hdc);
int iOldState = SaveDC(hdcMem); // save everything the HDC currently has selected
SelectObject(hdcMem, g_hbmBoard);
GetObject(g_hbmBoard, sizeof(bm), &bm);
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, g_hbmGreenLight);
GetObject(g_hbmGreenLight, sizeof(bm), &bm);
BitBlt(hdc, screenResW - 59, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, g_hbmWorkingLight);
GetObject(g_hbmWorkingLight, sizeof(bm), &bm);
BitBlt(hdc, screenResW - 94, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, g_hbmWorkingIndicator);
GetObject(g_hbmWorkingIndicator, sizeof(bm), &bm);
BitBlt(hdc, screenResW - 129, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
RestoreDC(hdcMem, iOldState); // restore everything the HDC originally had selected
DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
// who owns g_hbmBoard? ConvertIplImageToHBITMAP() creates
// a new HBITMAP, so you need to free it with DeleteObject()
// before calling ConvertIplImageToHBITMAP() again. It would
// be better to create g_hbmBoard one time outside of WM_PAINT,
// recreate g_hbmBoard only when the source image actually changes,
// and then re-use g_hbmBoard as-is inside of WM_PAINT...
// ConvertIplImageToHBITMAP() really does not belong in WM_PAINT...
//
//DeleteObject(g_hbmBoard);
}
I need to draw text to byte array, to convert it later to DirectX11 texture.
I was trying many thinks, for example like this:
HDC hdc= GetDC( g_hWnd );
int w= 600;
int h= 450;
unsigned* buf= new unsigned [w*h];
for( int a=0;a<w*h;a++)buf[a]= 0x0;
HBITMAP hbmp= CreateBitmap( w, h, 1, 4*8, buf );
if(!hbmp)throw "error bmp";
HDC vhdc= CreateCompatibleDC( hdc );
if(!vhdc)throw "error vhdc";
SelectObject( vhdc, hbmp );
TextOut( vhdc, 0, 0, L"TEST", 4 );
But after that buf is still empty.
I need it for intro 64KB, so I can not use big libraries.
Here is another not working code that I tried:
unsigned* buf= new unsigned [w*h];
for( int a=0;a<w*h;a++)buf[a]= 0x0;
HDC vhdc= CreateCompatibleDC( hdc ); if(!vhdc)throw "vhdc is hard";
HBITMAP hbmp= CreateCompatibleBitmap( hdc, w, h );
BITMAPINFO bmi = {{sizeof(BITMAPINFOHEADER),w,-h,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0}};
SelectObject( vhdc, hbmp );
TextOut( vhdc, 0, 0, L"TEST", 4 );
BITMAPINFO bmpi;
ZeroMemory( &bmpi, sizeof(bmpi) );
//GetDIBits(vhdc, hbmp, 0, h, buf, &bmpi, NULL);
GetDIBits(vhdc, hbmp, 0, h, buf, &bmpi, BI_RGB);
I was using vhdc and hdc as GetDIBits argument, non of it work.
jlahd already have answered the question.
But I will post working code in case anybody need it.
HDC hdc= GetDC( g_hWnd ); /// g_hWnd is my windows handle type HWND
int w= 1024;
int h= 768;
unsigned* buf= new unsigned [w*h];
HDC vhdc= CreateCompatibleDC( hdc ); if(!vhdc)throw "error with vhdc";
HBITMAP hbmp= CreateCompatibleBitmap( hdc, w, h );
BITMAPINFO bmpi = {{sizeof(BITMAPINFOHEADER),w,-h,1,32,BI_RGB,0,0,0,0,0},{0,0,0,0}};
SelectObject( vhdc, hbmp );
TextOut( vhdc, 10, 10, L"HELLO WORLD", 11 );
GetDIBits(vhdc, hbmp, 0, h, buf, &bmpi, BI_RGB);
After that code buf store data with image with "HELLO WORLD" drawed on it.
CreateBitmap only uses the given data as input. The buffer is not updated when you draw on it. You should use CreateDIBSection instead.
I have a question similar to this, How to display text in system tray icon with win32 API?
I tried his solution but it's not working for me. I get a small 4x16 white image as the system icon instead of text and I can't understand why.
I'm not using MFC/.NET just win32 api.
void UpdateIcon(HWND hWnd){
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = 100;
nid.hIcon = CreateSmallIcon(hWnd);
nid.uFlags = NIF_ICON;
Shell_NotifyIcon(NIM_MODIFY, &nid);
}
HICON CreateSmallIcon( HWND hWnd )
{
static TCHAR *szText = TEXT ( "100" );
HDC hdc, hdcMem;
HBITMAP hBitmap = NULL;
HBITMAP hOldBitMap = NULL;
HBITMAP hBitmapMask = NULL;
ICONINFO iconInfo;
HFONT hFont;
HICON hIcon;
hdc = GetDC ( hWnd );
hdcMem = CreateCompatibleDC ( hdc );
hBitmap = CreateCompatibleBitmap ( hdc, 16, 16 );
hBitmapMask = CreateCompatibleBitmap ( hdc, 16, 16 );
ReleaseDC ( hWnd, hdc );
hOldBitMap = (HBITMAP) SelectObject ( hdcMem, hBitmap );
PatBlt ( hdcMem, 0, 0, 16, 16, WHITENESS );
// Draw percentage
hFont = CreateFont (12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
TEXT ("Arial"));
hFont = (HFONT) SelectObject ( hdcMem, hFont );
TextOut ( hdcMem, 0, 0, szText, lstrlen (szText) );
SelectObject ( hdc, hOldBitMap );
hOldBitMap = NULL;
iconInfo.fIcon = TRUE;
iconInfo.xHotspot = 0;
iconInfo.yHotspot = 0;
iconInfo.hbmMask = hBitmapMask;
iconInfo.hbmColor = hBitmap;
hIcon = CreateIconIndirect ( &iconInfo );
DeleteObject ( SelectObject ( hdcMem, hFont ) );
DeleteDC ( hdcMem );
DeleteDC ( hdc );
DeleteObject ( hBitmap );
DeleteObject ( hBitmapMask );
return hIcon;
}
I don't have windows installed currently so i cannot check if this will work better, but i found potential problem - from MSDN documentation of CreateIconIndirect function:
The application must continue to manage the original bitmaps and delete them when they are no longer necessary.
Seems like you are deleting bitmaps too soon.
You need to set background and possibly foreground colors:
SetTextColor( hdcMem, 0x00FF0000 ); // 0x00bbggrr, not rrggbb !!
SetBkMode( hdcMem, TRANSPARENT ); // VERY IMPORTANT
I think DeleteDC ( hdc ); is not needed here as you used GetDC().