Bitmap manipulation in C++ on Windows - c++

I have myself a handle to a bitmap, in C++, on Windows:
HBITMAP hBitmap;
On this image I want to do some Image Recognition, pattern analysis, that sort of thing. In my studies at University, I have done this in Matlab, it is quite easy to get at the individual pixels based on their position, but I have no idea how to do this in C++ under Windows - I haven't really been able to understand what I have read so far. I have seen some references to a nice looking Bitmap class that lets you setPixel() and getPixel() and that sort of thing, but I think this is with .net .
How should I go about turning my HBITMAP into something I can play with easily? I need to be able to get at the RGBA information. Are there libraries that allow me to work with the data without having to learn about DCs and BitBlt and that sort of thing?

You can use OpenCV library as a full image processing tool.
You can also use MFC's CImage or VCL's TBitmap just to extract pixel values from HBITMAP.

Gdiplus::Bitmap* pBitmap = Gdiplus::Bitmap::FromHBITMAP( hBitmap, NULL );
Gdiplus::Color pixel_color;
pBitmap->GetPixel( x, y, &pixel_color ); // read pixel at x,y into pixel_color
// ...
delete pBitmap; // do not forget to delete

Try this using GetPixel from GDI:
COLORREF GetBitmapBixel(HBITMAP hBitmap, int xPos, int yPos)
{
HDC hDC = GetDC(NULL);
HDC hMemDC = CreateCompatibleDC(hDC);
COLORREF pixelColor;
HBITMAP hOld = (HBITMAP)SelectObject(hMemDC, hBitmap);
pixelColor = ::GetPixel(hMemDC, xPos, yPos);
SelectObject(hMemDC, hOld);
DeleteDC(hMemDC);
ReleaseDC(NULL, hDC);
return pixelColor;
}

With:
DIBSECTION ds;
::GetObject(hbmp/*your HBITMAP*/, sizeof DIBSECTION, &ds);
you will get all you need (including pixel format and pixel buffer adress) in ds.dsBm.
see the doc

Related

Create a GDI Rectangle image

I must be doing something wrong or have missed something because all I actually want to is render a rectangle into a bitmap, so that I can CreateWindowEx() on it. Does anyone know what I'm missing?
HDC hdc = GetDC(hWnd);
// Create Pen and brush for the rectangle
HPEN pn = CreatePen(style, stroke, pen);
HBRUSH br = CreateSolidBrush(brush);
// Create a compatible bitmap and DC from the window DC with the correct dimensions
HDC bm_hdc = CreateCompatibleDC(hdc);
HBITMAP hImage = CreateCompatibleBitmap(bm_hdc, sz.x, sz.y);
// Select the bitmap, pen, brush into the DC
HGDIOBJ bm_obj = SelectObject(bm_hdc, hImage);
HGDIOBJ pn_obj = SelectObject(bm_hdc, pn);
HGDIOBJ br_obj = SelectObject(bm_hdc, br);
// Draw the rectangle into the compatible DC with the bitmap selected
::Rectangle(bm_hdc, xPos, yPos, xPos + xSize, yPos + ySize);
// Restore the old selections
SelectObject(bm_hdc, br_obj);
SelectObject(bm_hdc, pn_obj);
SelectObject(bm_hdc, bm_obj);
// Delete the not needed DC, pen and brush
DeleteDC(bm_hdc);
DeleteObject(br);
DeleteObject(pn);
ReleaseDC(hWnd, hdc);
// Create the window and send a message to set the static image
HWND win = CreateWindow(TEXT("STATIC"), NULL, WS_CHILD | SS_BITMAP | WS_VISIBLE, pos.x, pos.y, sz.x, sz.y, hWnd, NULL, hInst, NULL)));
HGDIOBJ obj = (HGDIOBJ)SendMessage(win, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);
// Delete the old image
if (obj)
DeleteObject(hImage);
Hummmm... but this doesn't work... All I get is a completely black area and not the rectangle that I have drawn. Any ideas why? Do I need to create another DC and BitBlt() between device contexts?
Thanks for all the help everyone, but I've actually solved it myself and it was SUCH a silly mistake too... Consider this line...:-
::Rectangle(bm_hdc, xPos, yPos, xPos + xSize, yPos + ySize);
Nothing wrong with that at first glance, right? WRONG! If you look at my code, I create a compatible bitmap of the required size to contain my rectangle and try to render the rectangle into this bitmap (which is selected into the DC).
But... WHERE in the bitmap am I rendering? xPos and yPos are window positions of the rectangle, but I'm not rendering to the Window DC am I?!? d'oh! That's right, xPos and yPos should both be 0 because I'm rendering into a bitmap of the correct size and it's when the Window is displayed that xPos and yPos should contain screen coordinates!
Wow... what a dumb mistake and thanks for the nice spot on the HDC from the Window rather than from the compatible DC. I did know that a memory DC has a 1bit depth, but still made that classic blunder. Thanks everyone.
Try changing this line HBITMAP hImage = CreateCompatibleBitmap(bm_hdc, sz.x, sz.y); into this :
HBITMAP hImage = CreateCompatibleBitmap(hdc, sz.x, sz.y);
Paul Watt wrote excellent articles for GDI and image composition with MsImage32.dll.
I am reffering you to this article because it addresses your problem, and here are the relevant quotes and code snippets:
The memory DC is initialized with a mono-chromatic 1x1 pixel bitmap by default.
Avoid a Common Mistake
Before we get too far away from code where I showed you what you need to start running, I want to make sure you are holding this new pair of scissors safely. Do not use a Memory DC with a call to CreateCompatibleBitmap.
...
// You may be tempted to do this; DON'T:
HDC hMemDC = ::CreateCompatibleDC(hDC);
// DON'T DO THIS
// |
// V
HBITMAP hBmp = ::CreateCompatibleBitmap(hMemDC, width, height);
...
// TIP: Try to use the same DC to create
// the Bitmap which you used to create the memory DC.
Remember the part about how The memory DC is initialized with a mono-chromatic 1x1 pixel bitmap by default?!
As for the remarks of member Raymond Chen I believe he is also right, but since you said that your actual code is different this is the only thing I can see as a mistake.
Hopefully this helps.
Best regards.

Combine StretchBlt and TransparentBlt properly, so transparent bitmap can be created properly

INTRODUCTION AND RELEVANT INFORMATION:
Recently, I have asked, here in SO, a question about scaling a bitmap properly, so it can keep the quality of the picture:
Bitmap loses quality when stretched/shrinked on buttons background.
I have tried to employ a suggestion made in a comment, to use `StretchBlt, so I have made a small demo program.
It did improve the bitmaps sharpness, after I have set stretch mode to BLACKONWHITE.
I would like to try to make the portion of the bitmap, with the certain color-say black for example, transparent.
I have used TransparentBlt before, but I don't know how to do it now.
PROBLEM:
In order to preserve the sharpness of the picture, I need to StretchBlt it in the memory DC, with stretch mode being BLACKONWHITE.
The problem is that I do not know how to Blt it transparently into main window's DC.
Here is a code snippet from the demo app:
case WM_PAINT:
{
// main window's DC
hdc = BeginPaint(hWnd, &ps);
// main window's client rectangle
RECT r;
GetClientRect( hWnd, &r );
// memory DC for double buffering
HDC MemDC = CreateCompatibleDC( hdc );
// fill it with test brush
FillRect( MemDC, &r, (HBRUSH)GetStockObject( GRAY_BRUSH ) );
// select loaded bitmap into memory DC
HBITMAP old = (HBITMAP)SelectObject( MemDC, bmp );
// get bitmaps dimensions
BITMAP b;
GetObject( bmp, sizeof(BITMAP), &b );
// needed to preserve bitmap's sharpness
SetStretchBltMode( hdc, BLACKONWHITE );
StretchBlt( hdc, 0, 0, r.right - r.left, r.bottom - r.top,
MemDC, 0, 0, b.bmWidth, b.bmHeight, SRCCOPY );
/* TransparentBlt( ... ); call should go here,
so I can make portion of the bitmap transparent,
in order for the gray brush can be seen */
// cleanup
SelectObject( MemDC, old );
DeleteDC(MemDC);
EndPaint(hWnd, &ps);
}
return 0L;
break;
QUESTION:
How to modify the above code, so a bitmap can be transparent, in order for test brush to be seen ?
The original image is bellow.
I just need to use TransparentBlt( ..., RGB( 0, 0, 0 ) ); to make it transparent in black areas.
The example picture that shows result:
MY EFFORTS:
Browsing through Internet, I have found only simple tutorials, regarding double buffering.
I haven't found anything like this, but to be honest, I am inexperienced in WIN32 API, so I don't know how to phrase the question properly, in order to get better search results.
If further information is required, ask for it and I will supply it.
It is omitted to keep the question short.
You Need to create a mask use specific raster operations to copy only the Pixels were the mask is defined.
http://www.winprog.org/tutorial/transparency.html
The next code is MFC, but you can easily extract and convert the MFC objects into the Standard GDI operations.
http://www.codeproject.com/Articles/703/Drawing-Transparent-Bitmap-with-ease-with-on-the-f

MSPaint-like app writing. How to do BitBlt right?

I'm writing now simple mspaint-like program in C++ using windows.h (GDI). For my program I need only pen tool. So, I need to store somewhere main window's picture (for ex. in memory HDC and HBITMAP) to draw it after in WM_PAINT message.
When I first have to store window's HDC to my memory HDC and HBITMAP? In what message I should store window? For example, I think we can't do it in WM_CREATE because we have no window yet.
What is the difference between PatBlt and BitBlt? What should I use for my app?
How to copy window's HDC content to my memory HDC and Bitmap? I'm trying to do something like this:
LPRECT lpRect;
GetClientRect(hwnd, lpRect);
width = lpRect->right - lpRect->left;
height = lpRect->bottom - lpRect->top;
HDC hDC = GetDC(hwnd);
memoryDC = CreateCompatibleDC(hDC);
memoryBitmap = CreateCompatibleBitmap(hDC, width, height);
SelectObject(memoryDC, memoryBitmap);
PatBlt(memoryDC, 0, 0, width, height, PATCOPY);
ReleaseDC(hwnd, hDC);
But this don't work: program crashes.
How to restore window in WM_PAINT after that?
How to clear my window with white color?
1: I would recommend you lazy load your off-screen canvas as late as possible. If you need it in WM_PAINT and you haven't created it yet, create it then. If you need it at the point someone begins drawing, create it then. If it exists when you need it, then use it.
2: PatBlt fills a region of a bitmap using the device context's current brush. Brushes define patterns, which is why it's called PatBlt. BitBlt copies data from a source bitmap to a destination bitmap. You would use a BitBlt when you wanted to move the image from the off-screen bitmap to the frame buffer.
3: The lpRect parameter of GetClientRect is an output parameter. That means you have to supply the memory. In this case, GetClientRect is trying to write the rectangle to a null pointer and causing the crash.
RECT clientRect;
GetClientRect(hwnd, &clientRect);
width = clientRect.right - clientRect.left;
height = clientRect.bottom - clientRect.top;
WM_PAINT: seems to be the best place to create the memory hdc. You can do something like this
WM_PAINT:
if (!first_paint)
{
...code
first_paint = true;
}
...more code
break;

How to find out DC's dimensions?

Let's say I have a handle to device context (naturally, in Windows environment):
HDC hdc;
How can I get the width and height of it?
A device context (DC) is a structure that defines a set of graphic objects and their associated attributes, and the graphic modes that affect output.
By width and height I'm guessing you are referring to the bitmap painted ?
If so then i guess you can try the following :
BITMAP structBitmapHeader;
memset( &structBitmapHeader, 0, sizeof(BITMAP) );
HGDIOBJ hBitmap = GetCurrentObject(hDC, OBJ_BITMAP);
GetObject(hBitmap, sizeof(BITMAP), &structBitmapHeader);
//structBitmapHeader.bmWidth
//structBitmapHeader.bmHeight
I also know little about GDI, but it seems GetDeviceCaps might do the trick.
This simple piece of code I use always to get the dimensions of the rendering area, when I have only the HDC.
First, you must get a HWND from the HDC - is simple, then you can get the client rect of this HWND:
RECT rcCli;
GetClientRect(WindowFromDC(hdc), &rcCli);
// then you might have:
nWidth = rcCli.right-rcCli.left;
nHeight = rcCli.bottom-rcCli.top;
As a disclaimer, I know nothing about GDI or what you have to work with in your application. I'm just trying to be helpful if possible.
That said, I found a link which seems to suggest that it's appropriate to use GetClientRect to get the size of the drawing area:
RECT clientRect;
GetClientRect(hWnd,&clientRect);
http://www.toymaker.info/Games/html/gdi.html#winsize
You could WindowFromDC(...) to get the DC's window if it's associated with a window. You could then use #KevinK's answer to get the client rect from this.
but if get Calculator' window_dc dimension, it will failed at “GetCurrentObject” or "GetObject", i think maybe the window attribute include "ws_ex_noredirectionbitmap", how to get dismension in this case?
HDC win_dc = ::GetWindowDC(hwnd);
BITMAP bm = { 0 };
HGDIOBJ hBitmap = GetCurrentObject(win_dc, OBJ_BITMAP);
if (hBitmap)
{
if (GetObject(hBitmap, sizeof(BITMAP), &bm))
{
windc_dimension.cx = bm.bmWidth;
windc_dimension.cy = bm.bmHeight;
}
}

Win32 Device Context without Window

In my application i need to create HBITMAP objects to which I render and from where I copy the result.
I use the function "CreateDIBSection" to create these bitmaps, however this function required a DC (Device Context) as first parameter. Currently I get this by calling GetDC(hWnd) on the main windows handle (hWnd). But I would like to be able to create HBITMAPS without the requirement of having an application window, without some kind of in memory DC, Is this possible?
CreateCompatibleDC(NULL) will create you a device context that is compatible with the screen - which sounds like it would be ideal in the situation.
You can get one with CreateDC for the display:
HDC hDc = CreateDC(L"DISPLAY", NULL, NULL, NULL);
Cleanup with DeleteDC(). It is only used to initialize the palette for bitmaps with indexed format. NULL might work if you don't use such a format, never tried it.
Then there's GDI+, #include <gdiplus.h> and the Bitmap class...
try this. it worked.
HDC hdcScreen = ::GetDC( NULL );
HDC hdcMemDC = ::CreateCompatibleDC(hdcScreen);
HBITMAP hbmScreen = ::CreateCompatibleBitmap(hdcScreen, cx, cy);
HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(hdcMemDC, hbmScreen);
MyImageDraw(hdcMemDC, ...);
// The drawing image is held in hBitmap. You can save it
HBITMAP hBitmap = (HBITMAP)::SelectObject(hdcMemDC, hOldBitmap);
// save The trend image into c:\test.bmp
PBITMAPINFO pbi = CreateBitmapInfoStruct(hBitmap);
CreateBMPFile("C:\\Temp\\test.bmp", pbi, hBitmap, hdcMemDC);
//Clean up
::DeleteObject(hbmScreen);
::DeleteObject(hdcMemDC);
::ReleaseDC( NULL, hdcScreen );