WIN32,GetBitmapBits与GetPixel

本文介绍了两种获取位图数据的方法:GetBitmapBits 和 GetPixel。GetBitmapBits 函数用于复制指定设备相关位图的位图位到缓冲区,而 GetPixel 则用于获取指定坐标处的 RGB 值。通常情况下,GetBitmapBits 的效率比 GetPixel 高出至少十倍,因此在频繁读取像素值时推荐使用前者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看看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拷贝数据。




def jietu(self,hwnd): # 获取窗口位置 left, top, right, bottom = win32gui.GetWindowRect(hwnd) width = right - left height = bottom - top # 创建DC对象 hwndDC = win32gui.GetWindowDC(hwnd) mfcDC = win32ui.CreateDCFromHandle(hwndDC) saveDC = mfcDC.CreateCompatibleDC() # 创建位图对象 saveBitMap = win32ui.CreateBitmap() saveBitMap.CreateCompatibleBitmap(mfcDC, width, height) # 将位图选入DC对象 saveDC.SelectObject(saveBitMap) # 截屏 saveDC.BitBlt((0, 0), (width, height), mfcDC, (0, 0), win32con.SRCCOPY) # 获取位图数据 bmpinfo = saveBitMap.GetInfo() bmpstr = saveBitMap.GetBitmapBits(True) # 将位图数据转换为numpy数组 img = np.frombuffer(bmpstr, dtype='uint8') img.shape = (bmpinfo['bmHeight'], bmpinfo['bmWidth'], 4) cv2.imwrite('1.bmp', img) # 释放资源 win32gui.DeleteObject(saveBitMap.GetHandle()) saveDC.DeleteDC() mfcDC.DeleteDC() win32gui.ReleaseDC(hwnd, hwndDC) # 返回截屏结果 return img 将img 传入def bisefs(self,loadjietu,mubiao,suku,wucha): jjieguo=0 for sekus in suku: yanse=sekus[2] red = int(yanse[4:6], 16) green = int(yanse[2:4], 16) blue = int(yanse[0:2], 16) #RGBneirongzhi=(red,green,blue) xxx=int(sekus[0]) yyy=int(sekus[1]) yuanse=loadjietu.getpixel((int(sekus[0]),int(sekus[1]))) #RGByuanse = (yuanse[2], yuanse[1], yuanse[0]) secha = math.sqrt((red-yuanse[2])**2 + (green-yuanse[1])**2 + (blue-yuanse[0])**2) if secha <= wucha: jjieguo=jjieguo+1 if jjieguo==len(suku): return True,xxx,yyy else: return False进行比色并返回结果
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值