I followed this code here to take a screenshot. It puts stuff onto an HDC. I was wondering how to get stuff off from this HDC as an array of pixel data. I want to copy it to clipboard and also draw it to a HTML5 canvas.
Do I have to run GetPixel for every point on the HDC, is this the only way to get an array of the bitmap?
HBITMAP MakePrintScreen()
{
HWND hWindow = GetDesktopWindow();
HDC hdcScreen = GetDC(hWindow);
RECT rect;
HBITMAP hbmC;
GetClientRect(hWindow,&rect);
if((hbmC = CreateCompatibleBitmap(hdcScreen,rect.right,rect.bottom)) != NULL)
{
HDC hdcC;
if((hdcC = CreateCompatibleDC(hdcScreen)) != NULL)
{
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcC,hbmC);
BitBlt(hdcC,0,0,rect.right,rect.bottom,hdcScreen,0,0,SRCCOPY);
SelectObject(hdcC,hbmOld);
DeleteDC(hdcC);
}
}
ReleaseDC(hWindow,hdcScreen);
return hbmC;
}
After you have deselected the bitmap from the device context you call GetDIBits to retrieve the bits from the bitmap.
Related
I am trying to capture a specific Window in Windows 10.1. The problem is that this window is a UWP application. I found the class and caption of the window using Spy++. However, when I capture the UWP window with my Screenshot function, the output is a black bitmap. My function works for any other windows except this specific one.
The class of the UWP application is Windows.UI.Core.CoreWindow
Here is my code:
HDC hScreenDC = GetWindowDC(ProgramData->TargetWnd);
RECT Rect = {0};
GetWindowRect(ProgramData->TargetWnd, &Rect);
INT Width = Rect.right - Rect.left;
INT Height = Rect.bottom - Rect.top;
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, Width, Height);
HBITMAP hOldBitmap = (HBITMAP) SelectObject(hMemoryDC, hBitmap);
BitBlt(hMemoryDC, 0, 0, Width, Height, hScreenDC, 0, 0, SRCCOPY | CAPTUREBLT);
hBitmap = (HBITMAP) SelectObject(hMemoryDC, hOldBitmap);
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
Why is the window not being captured properly and how can I capture it?
I am attempting to use GDI+ in my C application to take a screenshot and save it as JPEG. I am using GDI+ to convert the BMP to JPEG but apparently when calling the GdiplusStartup function, the return code is 2(invalid parameter) instead of 0:
int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
//if(GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL) != 0)
// printf("GDI NOT WORKING\n");
printf("%d",GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL));
HDC hdc = GetDC(NULL); // get the desktop device context
HDC hDest = CreateCompatibleDC(hdc); // create a device context to use yourself
// get the height and width of the screen
int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
// create a bitmap
HBITMAP hbDesktop = CreateCompatibleBitmap( hdc, width, height);
// use the previously created device context with the bitmap
SelectObject(hDest, hbDesktop);
// copy from the desktop device context to the bitmap device context
// call this once per 'frame'
BitBlt(hDest, 0,0, width, height, hdc, 0, 0, SRCCOPY);
// after the recording is done, release the desktop context you got..
ReleaseDC(NULL, hdc);
// ..and delete the context you created
DeleteDC(hDest);
SaveJpeg(hbDesktop,"a.jpeg",100);
GdiplusShutdown(gdiplusToken);
return 0;
}
I am trying to figure out why the GdiplusStartup function is not working.
Any thoughts?
Initialize gdiplusStartupInput variable with the following values: GdiplusVersion = 1, DebugEventCallback = NULL, SuppressBackgroundThread = FALSE, SuppressExternalCodecs = FALSE
According to MSDN article GdiplusStartup function http://msdn.microsoft.com/en-us/library/windows/desktop/ms534077%28v=vs.85%29.aspx
GdiplusStartupInput structure has default constructor which initializes the structure with these values. Since you call the function from C, constructor is not working and structure remains uninitialized. Provide your own initialization code to solve the problem.
// As Global
ULONG_PTR gdiplusToken;
// In top of main
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&programInfo.gdiplusToken, &gdiplusStartupInput, NULL);
works for me.
HBITMAP DisplayMap(HDC hThisDC){
HDC hDC=CreateCompatibleDC(hThisDC);
BITMAPINFOHEADER bi;
bi.biSize=sizeof(BITMAPINFOHEADER);
bi.biWidth=670;
bi.biHeight=540;
bi.biPlanes=1;
bi.biBitCount=24;
bi.biCompression=BI_RGB;
int lineSize=((((bi.biWidth*bi.biBitCount)+31)&~31)>>3);
bi.biSizeImage=lineSize*bi.biHeight;
unsigned char* data=(unsigned char*)malloc(bi.biSizeImage);
int off=0;
for(int y=0;y<bi.biHeight;y++){
for(int x=0;x<bi.biWidth;x++){
data[off+3*x]=255;
data[off+3*x+1]=0;
data[off+3*x+2]=127;
}
off+=lineSize;
}
HBITMAP hBitmap=CreateDIBitmap(hDC,&bi,CBM_INIT,data,(BITMAPINFO*)&bi,DIB_RGB_COLORS);
SetDIBits(hDC,hBitmap,0,bi.biHeight,data,(BITMAPINFO*)&bi,DIB_RGB_COLORS);
return hBitmap;
}
//
HBITMAP hBitmap=DisplayMap(GetDC(hwnd));
if(hBitmap==NULL||true){
char* str=(char*)malloc(15);
sprintf(str,"ERROR: %d!",GetLastError());
MessageBox(NULL,str,"ALERT",MB_OK|MB_ICONEXCLAMATION);
}
ShowWindow (hwnd, nFunsterStil);
SendMessage(hFrame, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);
//
If I use "(HBITMAP)LoadImage()" to display it, it shows no problem. But as I'm using my function "DisplayMap()" which is supposed to create a HBITMAP out of nothing it doesn't work. The script shows no erros and the "hBitmap" return a valid HBITMAP (not NULL), but don't matter the color I choose to fill the bitmap, it keeps displaying the same full-black image.
I searched for a solution in Google through many sources and I wasn't able to figure out the problem. I thank for any help, has been a long time I don't work with c.The idea is to create and display an image sequence which shall be generate just in time without loading anything from files.
Why do you GetDC from hwnd, but STM_SETIMAGE to hFrame. Try GetDC from hFrame.
When I use SetBkMode(hdc, TRANSPARENT); in the code below, I got the following effect when I resize the main window (and hence when the child receives the WM_PAINT message):
The problem is : When I resize the main window, The old area of "Find:" shoule be erased, I guess. But it just remains there.
If I don't use SetBkMode(hdc, TRANSPARENT);, I don't have this problem. It looks like:
, i.e it has white background. Furthermore, if I use SetBkMode(hdc, TRANSPARENT);, it looks like the same as above, before I resize the main window. So I don't think SetBkMode(hdc, TRANSPARENT); works here.
the hwnd is a static child with style SS_BITMAP.
Do you know why this issue occurs?
switch (message) {
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, gDefaultGuiFont);
SetBkMode(hdc, TRANSPARENT);
RECT rc;
GetClientRect(hwnd, &rc);
DrawText(hdc, _TR("Find:"), -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd, &ps);
return 0;
.............
}
Try to use "fixed" rectangles. For example
RECT rc;
GetClientRect(hwnd, &rc);
rc.left += ...; rc.top += ...; // shift up-left point
DrawText(hdc, _TR("Find:"), -1, &rc, DT_SINGLELINE | DT_LEFT | DT_TOP);
The idea is you draw text in wrong position (once) and in right position (twice) while backgound updated only once. Can't say more on part of code.
The problem is that windows is not updating the control (in time) that's behind your static control, you are now responsible for it's contents. So you want to use the background provided by the parent. Well just ask the parent to draw it for you in the child window:
RECT rc;
GetClientRectRelative(m_hWnd, GetParent(m_hWnd), &rc);
SetWindowOrgEx(m_mdc, rc.left, rc.top, NULL);
SendMessage(GetParent(m_hWnd), WM_PAINT, (WPARAM)(HDC)m_mdc);
SetWindowOrgEx(m_mdc, 0, 0, NULL);
In which
bool GetClientRectRelative(HWND hWnd, HWND hWndRelativeTo, RECT *pRect)
{
RECT rcWnd, rcRelativeTo;
if (!GetClientRect(hWnd, &rcWnd) ||
!ClientToScreen(hWnd, (POINT*)&rcWnd) ||
!ClientToScreen(hWnd, (POINT*)&rcWnd + 1) ||
!GetClientRect(hWndRelativeTo, &rcRelativeTo) ||
!ClientToScreen(hWndRelativeTo, (POINT*)&rcRelativeTo) ||
!ClientToScreen(hWndRelativeTo, (POINT*)&rcRelativeTo + 1))
return false;
pRect->top = rcWnd.top - rcRelativeTo.top;
pRect->left = rcWnd.left - rcRelativeTo.left;
pRect->right = rcWnd.right - rcRelativeTo.left;
pRect->bottom = rcWnd.bottom - rcRelativeTo.top;
return true;
}
Now draw anything you like, I suggest you'd use the TRANSPARENT background mode.
Please create all your child windows with the styles WS_CLIPCHILDREN and WS_CLIPSIBLINGS, then these problems will become apparent immediately and you avoid flicker.
I'm trying to get to the bottom of a memory leak in an application written in C and running on Windows CE 6.0. I suspect that the issue MAY be related to the handling of the paint event of the window. In pseudo code it looks like this.
LRESULT CALLBACK HandlePaint(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint (hWnd, &ps);
HFONT logfont;
FONTINFO font1, font2;
memset(&logfont, 0, sizeof(LOGFONT));
//set font options for font1.
font1 = CreateFontIndirect(&logfont);
memset(&logfont, 0, sizeof(LOGFONT));
//set font options for font2.
font2 = CreateFontIndirect(&logfont);
for(int i = 0; i <= SOME_NUMBER; i++)
{
DrawStuff(hdc, font1);
DrawStuff(hdc, font2);
}
EndPaint (hWnd, &ps);
}
INT DrawStuff(HDC hdc, HFONT font)
{
HPEN pen = CreatePen(PS_SOLID, borderWidth, bordercolor);
HBRUSH brush = CreateSolidBrush(backcolor);
SelectObject (hdc, pen);
SelectObject (hdc, brush);
SelectObject(hdc, font);
SetTextColor (hdc, forecolor);
SetBkColor (hdc, backcolor);
DrawText (hdc, pChar, wcslen(pChar), prect, DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_NOPREFIX);
DeleteObject(font);
DeleteObject(brush);
DeleteObject(pen);
}
I've noticed in the examples I've seen for windows graphics that there seems to be a pattern for most grapics objects of:
HBRUSH brush = CreateBrush();
SelectObject(hdc, brush);
// use brush
DeleteObject(brush);
However, as you can see in the example above with the fonts, each font is being created once, and then Selected/Deleted multiple times. I'm not sure what the implications are of doing that. Would there be a reason to suspect a memory leak here?
Thanks!
I agree with #pmg's comment that the creator of the Form should be the destroyer of the font, not the DrawStuff callee.
Also bear in mind that SelectObject returns the original item in the DC and you should always return that object when you're done, e.g.:
HPEN newPen = CreatePen(...);
HPEN oldPen = SelectObject(hdc, newPen);
// do stuff
// clean up
SelectObject(hdc, oldPen); // <-- note this line
DeleteObject(newPen);