开发者

SetBitmapBits not setting Captured HBITMAP on Window

开发者 https://www.devze.com 2023-02-24 00:35 出处:网络
I want to get a 500x500 HBITMAP of my screen from 0x0 (top-left) and draw it on my window. Here goes my code. SaveBitmap() Saves the HBITMAP and its working Fine.

I want to get a 500x500 HBITMAP of my screen from 0x0 (top-left) and draw it on my window. Here goes my code. SaveBitmap() Saves the HBITMAP and its working Fine.

int scrnw = 500;
int scrnh = 500;

HDC shdc=GetWindowDC(NULL);
HWND win=WindowFromDC(shdc);

HDC cdc=CreateCompatibleDC(shdc);
HBITMAP temp=CreateCompatibleBitmap(shdc,scrnw,scrnh);
PAINTSTRUCT ps;

shdc=BeginPaint(win,&ps);
HBITMAP oldb=(HBITMAP)S开发者_开发技巧electObject(cdc,temp);
BitBlt(cdc,0,0,scrnw,scrnh,shdc,0,0,SRCCOPY);
SelectObject(cdc,oldb);
EndPaint(win,&ps);
char * buffer;
buffer=new char[scrnw*scrnh*4];
GetBitmapBits(temp,scrnw*scrnh*4,buffer);

SaveBitmap("C:\\scan.bmp", temp);

HDC hdc=GetWindowDC(hwnd);
HBITMAP scrn = CreateCompatibleBitmap(hdc,500,500);
SetBitmapBits(scrn,500*500*4,buffer);

The Problem is the Captured Image is not being drawn on the Window.Where am I missing ?


shdc is "forgetting" the screen when you perform shdc=BeginPaint(), so there's nowhere to get the image from.

Let it stay with shdc=GetWindowDC(NULL) and use another HDC for hdcMyPaint=BeginPaint(). After blitting to cdc, blit also to your hdcMyPaint. That should do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号