SetBitmapBits 未在窗口上设置捕获的 HBITMAP
我想从 0x0(左上角)获取屏幕的 500x500 HBITMAP 并将其绘制在我的窗口上。 这是我的代码。 SaveBitmap()
保存 HBITMAP 及其工作状态。
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)SelectObject(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);
问题是捕获的图像没有绘制在窗口上。我错过了哪里?
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)SelectObject(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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您执行
shdc=BeginPaint()
时,shdc
会“忘记”屏幕,因此无处可获取图像。让它保留
shdc=GetWindowDC(NULL)
并使用另一个 HDC 进行hdcMyPaint=BeginPaint()
。 blit 到cdc
后,也 blit 到您的hdcMyPaint
。应该可以做到这一点。shdc
is "forgetting" the screen when you performshdc=BeginPaint()
, so there's nowhere to get the image from.Let it stay with
shdc=GetWindowDC(NULL)
and use another HDC forhdcMyPaint=BeginPaint()
. After blitting tocdc
, blit also to yourhdcMyPaint
. That should do it.