WIN32,GetBitmapBits与GetPixel

先看看MSDN上的,

The GetBitmapBits function copies the bitmap bits of a specified device-dependent bitmap into a buffer.

LONG GetBitmapBits(
  _In_  HBITMAP hbmp,
  _In_  LONG    cbBuffer,
  _Out_ LPVOID  lpvBits
);

hbmp [in]

A handle to the device-dependent bitmap.

cbBuffer [in]

The number of bytes to copy from the bitmap into the buffer.

lpvBits [out]            A pointer to a buffer to receive the bitmap bits. The bits are stored as an array of byte values

获取位图数据,并将数据拷贝到缓冲区中(第三个参数),位图数据是跟具体设备相关的。一般获取的位图数据为32位,如果将其保存为jpg的话,需要转换成24位的才行,某些开源看暂时不支持32的rgb空间位图。


GetPixel:

The GetPixel function retrieves the red, green, blue (RGB) color value of the pixel at the specified coordinates.

COLORREF GetPixel(
  _In_ HDC hdc,
  _In_ int nXPos,
  _In_ int nYPos
);

获取点(x,y)下的像素值。

两者均可以获取图像数据,但是GetBitmapBits要比GetPixel快至少10倍,所以当频繁读取具体像素值时,最好用GetBitmapBits。以前做过一个项目,创建一个位图,然后在上面绘制文本,然后把结果保存为图片,这时,采用GetBitmapBits拷贝数据。




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Windows 上进行屏幕录制,需要使用 Win32 API。下面是一个简单的示例代码,可以使用 GDI 函数和 Windows 消息循环来捕获屏幕图像并保存为 AVI 文件。 ```python import win32api import win32con import win32gui import win32api import win32ui import struct # 定义窗口大小 width = 640 height = 480 # 初始化 GDI 和 Windows API hwnd = win32gui.GetDesktopWindow() dc = win32gui.GetWindowDC(hwnd) dcObj = win32ui.CreateDCFromHandle(dc) cDC = dcObj.CreateCompatibleDC() bmp = win32ui.CreateBitmap() bmp.CreateCompatibleBitmap(dcObj, width, height) cDC.SelectObject(bmp) # 打开 AVI 文件 AVIFileInit() pfile = AVIFileOpen("output.avi", modeCREATE, 0) stream = AVIFileCreateStream(pfile, by_fourcc('M', 'S', 'V', 'C'), 0, 0) # 开始录制 while True: # 获取屏幕图像 cDC.BitBlt((0, 0), (width, height), dcObj, (0, 0), win32con.SRCCOPY) # 将图像写入 AVI 文件 dib = bmp.GetBitmapBits(True) stream.Write(0, len(dib), dib) # 处理 Windows 消息 if win32api.GetKeyState(win32con.VK_ESCAPE) < 0: break # 停止录制 stream.Release() pfile.Release() AVIFileExit() # 释放资源 cDC.DeleteDC() dcObj.DeleteDC() win32gui.ReleaseDC(hwnd, dc) bmp.DeleteObject() ``` 需要注意的是,上述代码只是一个基本的示例,实际的屏幕录制程序需要更多的功能,例如帧率控制、压缩算法等。同时,在使用 Win32 API 进行屏幕录制时,还需要注意性能问题,尽可能减少对系统资源的占用,以免影响其他应用程序的正常运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值