I have two classes with windows in one program (the view, and dialog), that must show pictures.
The view take picture from disk like:
CClientDC dc(this);
GetClientRect(&rc);
dcMem.DeleteDC();
hBmp = NULL;;
m_bmpBack.DeleteObject();
hBmp = LoadImage(NULL,"d:/USBconn.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
GetObject ( hBmp, sizeof(bm), &bm );
m_bmpBack.Attach(hBmp);
dcMem.CreateCompatibleDC(&dc);
hbmpOld = (HBITMAP)dcMem.SelectObject(m_bmpBack);
dc.StretchBlt(rc.left, rc.top, rc.right, rc.bottom,&dcMem, 0, 0, (int)bm.bmWidth, (int)bm.bmHeight, SRCCOPY);
How to "BitBlt" image from this dc in to the dc in the other class and window?
Related
I have this code
// get the device context of the screen
HDC hScreenDC = GetDC(NULL);
// and a device context to put it in
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
// get a new bitmap
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);
// clean up
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
that generates an HBITMAP of the screen. It works perfectly most of the time, but if I try to run it when I have something on full screen on my browser (Youtube, for example), the image captured is not of the video, rather other program running on the background (Visual Studio for me). I suspect the problem is on getting the device context, but I've tried a few alternatives and had the same problems. How can I solve this?
Use DirectX method from this article: https://www.codeproject.com/Articles/5051/Various-methods-for-capturing-the-screen
After calling GetFrontBufferData you can simply save your bitmap to disk or dig out actual image data and use SetDIBits to assign it to HBITMAP.
I'm creating a new-UI walkthrough for my MFC application and want to highlight certain controls as the walkthrough proceeds. Specifically, I want to darken the whole window except the control I'm emphasizing.
I tried creating a partly-transparent black overlay using SetLayeredWindowAttributes, but this doesn't let me set a sub-area completely transparent. UpdateLayeredWindow can do this, but I'm not eager to create a BMP/PNG file for every control I need to highlight.
Can I create the transparency geometry dynamically? For example, can I draw bitmap transparency from scratch then load it into UpdateLayeredWindow?
I also need to be compatible with Windows 7 (despite its support EOL).
Follow-up:
Trying to paint transparent GDI+ regions, but doesn't work:
void ApplicationDlg::Highlight(const CRect& rect)
{
CRect wndRect;
GetWindowRect(&wndRect);
Gdiplus::Rect wndRectPlus(wndRect.left, wndRect.top, wndRect.Width(), wndRect.Height());
Gdiplus::Region wndRegion(wndRectPlus);
Gdiplus::Rect controlRectPlus(rect.left, rect.top, rect.Width(), rect.Height());
Gdiplus::Region highlightRegion(controlRectPlus);
wndRegion.Exclude(&highlightRegion);
Gdiplus::SolidBrush transparentBrush(Gdiplus::Color(0, 0, 0, 0));
Gdiplus::SolidBrush darkenBrush(Gdiplus::Color(128, 0, 0, 0));
CDC* pDCScreen = m_WalkthroughDlg.GetDC();
HDC hDC = CreateCompatibleDC(pDCScreen->m_hDC);
HBITMAP hBmp = CreateCompatibleBitmap(hDC, wndRect.Width(), wndRect.Height());
HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBmp);
Gdiplus::Graphics graphics(hDC);
graphics.FillRegion(&darkenBrush, &wndRegion);
graphics.FillRegion(&transparentBrush, &highlightRegion);
BLENDFUNCTION blend = {0};
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
SIZE sizeWnd = {wndRect.Width(), wndRect.Height()};
POINT ptSrc = {0,0};
m_WalkthroughDlg.UpdateLayeredWindow(pDCScreen, NULL, &sizeWnd, CDC::FromHandle(hDC), &ptSrc, NULL, &blend, ULW_ALPHA); // TODO cleanup FromHandle refs
m_WalkthroughDlg.BringWindowToTop();
SelectObject(hDC, hBmpOld);
DeleteObject(hBmp);
DeleteDC(hDC);
}
You can dynamically create a mask by using CRgn class: https://learn.microsoft.com/en-us/cpp/mfc/reference/crgn-class?view=vs-2019
It allows you to combine regions (if you need to highlight more than one area). You could then use FillRgn function to update the hdcSrc DC used in UpdateLayeredWindow.
Alternatively, if your highlights are rectangular, you could just draw rectangles on that hdcSrc.
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 want to catch a Desktop frame and store it in a HBITMAP struct.
Then, after creating a proper memory device context out of my application main window's device context, I would select the HBITMAP into it and the use StretchBlt to display the bitmap.
But this doesn't work as expected, because it just shows a black frame.
Both hdc and mem_hdc are respectively the device context and memory device context of main window initialized before.
Here's the code:
...
hDC desk_hdc, desk_mem_hdc;
BITMAP bitmap;
HBITMAP hbitmap;
desk_hdc = GetDC(NULL);
hbitmap = CreateCompatibleBitmap(desk_hdc, GetDeviceCaps(desk_hdc, HORZRES), GetDeviceCaps(desk_hdc, VERTRES));
GetObject(hbitmap, sizeof(BITMAP), &bitmap);
SelectObject(mem_hdc, hbitmap);
StretchBlt(hdc, 0, 0, 1024, 768, mem_hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY|CAPTUREBLT|NOMIRRORBITMAP);
...
The source dc of your StretchBlt operation is mem_hdc, which has a compatible uninitialized bitmap. That's why you get a black frame.
If you want to capture the desktop contents, you have to first copy it to the bitmap in your mem_hdc. Just after SelectObject do:
BitBlt( mem_hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, desk_hdc, 0, 0, SRCCOPY );
Is there a way to receive the colour values for each pixel in the client area of a window, with gdi?
As noted in comment by #JerryCoffin. Here's a simple example
hDC = GetDC(hwnd);
hBitmap = CreateCompatibleBitmap(hDC, width, height);
hMemDC = CreateCompatibleDC(hDC);
hOld = SelectObject(hMemDC, hBitmap);
BitBlt(hMemDC, 0, 0, width, height, hDC, x, y, SRCCOPY);
// Clean up
DeleteDC(hMemDC);
ReleaseDC(hwnd, hDC);
You should have a bitmap object selected into memory DC for which you can use GetPixel GDI function and then you can also extract the color values using GetRValue() , GetGValue() , and GetBValue() macros.