看流星社区

 找回密码
 注册账号
查看: 7413|回复: 3

[VB] VB原创纯代码全屏找图找色(支持色偏,透明图)

[复制链接]

该用户从未签到

发表于 2015-4-11 21:48:36 | 显示全部楼层 |阅读模式
N年前初识VB时写的代码,乱了点,各位将就看看把,如果有需求下次发上GDI后台的图色,等同于大漠GDI后台(某位大牛逆向大漠插件得到的方法,我有空就写了出来)
VB开发的图色识别速度上与C++开发的其实相差并不是很大,1440*900扫描一张图,都在稳定100MS内完成,只有在找透明图
时,遇到非透明色的颜色和待找大图中大部分颜色相同时,效率会远远低于C++,貌似循环套嵌效率没C++高。
(透明图指的是图片四个角颜色相同,默认图片中该色为透明图,忽略该种颜色的对比)

管理可精否?

找色部分


  1. Option Explicit

  2. '====================================================
  3. Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
  4. Private Declare Function SetDIBits Lib "gdi32" (ByVal HDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
  5. '====================================================

  6. '====================================================
  7. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long '获取句柄
  8. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long '获取图片数据

  9. Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal HDC As Long) As Long '释放DC

  10. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal HDC As Long) As Long
  11. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal HDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
  12. Private Declare Function DeleteDC Lib "gdi32" (ByVal HDC As Long) As Long
  13. Private Declare Function SelectObject Lib "gdi32" (ByVal HDC As Long, ByVal hObject As Long) As Long
  14. Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source

  15. Dim intX As Long
  16. Dim intY As Long
  17. Dim intZ As Long

  18. '颜色表
  19. Private Type RGBQUAD
  20.     rgbBlue As Byte
  21.     rgbGreen As Byte
  22.     rgbRed As Byte
  23.     rgbAlpha As Byte   '透明通道
  24. End Type

  25. Private Type BITMAPINFOHEADER
  26.     biSize As Long          '位图大小
  27.     biWidth As Long
  28.     biHeight As Long
  29.     biPlanes As Integer
  30.     biBitCount As Integer   '信息头长度
  31.     biCompression As Long   '压缩方式
  32.     biSizeImage As Long
  33.     biXPelsPerMeter As Long
  34.     biYPelsPerMeter As Long
  35.     biClrUsed As Long
  36.     biClrImportant As Long
  37. End Type

  38. Private Type BITMAPINFO
  39.     bmiHeader As BITMAPINFOHEADER
  40.     bmiColors As RGBQUAD
  41. End Type

  42. '图片文件头
  43. Dim BI As BITMAPINFO
  44. Dim BI1 As BITMAPINFO

  45. Public Function FindColorEx(Left As Long, Top As Long, Right As Long, Bottom As Long, color As String) As String
  46. Dim l() As String
  47. Dim ld As Long
  48. Dim W As Long, H As Long, i As Long, j As Long
  49. Dim RGB(3) As Long
  50. Dim fPic() As Byte
  51. l = Split(color, "|")
  52. Dim m As Long
  53. W = Right
  54. H = Bottom
  55. With BI1.bmiHeader
  56.     .biSize = Len(BI1.bmiHeader)
  57.     .biWidth = W
  58.     .biHeight = -H
  59.     .biBitCount = 32
  60.     .biPlanes = 1
  61. End With

  62. ReDim fPic(3, W - 1, H - 1)

  63.      Dim hBMPhDC
  64.      Dim hDCmem As Long
  65.      Dim Pic1Handle As Long
  66.      Dim hBmpPrev As Long
  67.      hBMPhDC = GetDC(0)
  68.      hDCmem = CreateCompatibleDC(hBMPhDC)
  69.      Pic1Handle = CreateCompatibleBitmap(hBMPhDC, Right, Bottom)
  70.      hBmpPrev = SelectObject(hDCmem, Pic1Handle)
  71.      BitBlt hDCmem, 0, 0, Right, Bottom, hBMPhDC, Left, Top, SRCCOPY
  72.      DeleteDC hDCmem

  73. i = GetDIBits(hBMPhDC, Pic1Handle, 0, H, fPic(0, 0, 0), BI1, 0)

  74. ReleaseDC 0, hBMPhDC
  75. For ld = 0 To UBound(l)
  76. For m = 0 To 2
  77.     RGB(m) = CLng("&H" & Mid(l(ld), m * 2 + 1, 2))
  78. Next
  79. '分析查找
  80. For j = 0 To H - 1
  81.     For i = 0 To W - 1
  82.                 If fPic(2, i, j) <> RGB(0) Then GoTo ExitLine:   'R
  83.                 If fPic(1, i, j) <> RGB(1) Then GoTo ExitLine:   'G
  84.                 If fPic(0, i, j) <> RGB(2) Then GoTo ExitLine:   'B
  85.         If FindColorEx = "" Then
  86.         FindColorEx = ld & "," & i & "," & j
  87.         Else
  88.         FindColorEx = FindColorEx & "|" & ld & "," & i & "," & j
  89.         End If
  90. ExitLine:
  91.     Next i
  92. Next j
  93. Next

  94. End Function


  95. Public Function FindColor(Left As Long, Top As Long, Right As Long, Bottom As Long, color As String) As String
  96. Dim l() As String
  97. Dim W As Long, H As Long, i As Long, j As Long
  98. Dim RGB(3) As Byte
  99. Dim fPic() As Byte
  100. Dim m As Long
  101. W = Right
  102. H = Bottom
  103. With BI1.bmiHeader
  104.     .biSize = Len(BI1.bmiHeader)
  105.     .biWidth = W
  106.     .biHeight = -H
  107.     .biBitCount = 32
  108.     .biPlanes = 1
  109. End With
  110. ReDim fPic(3, W - 1, H - 1)
  111.      Dim hBMPhDC
  112.      Dim hDCmem As Long
  113.      Dim Pic1Handle As Long
  114.      Dim hBmpPrev As Long
  115.      hBMPhDC = GetDC(0)
  116.      hDCmem = CreateCompatibleDC(hBMPhDC)
  117.      Pic1Handle = CreateCompatibleBitmap(hBMPhDC, Right, Bottom)
  118.      hBmpPrev = SelectObject(hDCmem, Pic1Handle)
  119.      BitBlt hDCmem, 0, 0, Right, Bottom, hBMPhDC, Left, Top, SRCCOPY
  120.      DeleteDC hDCmem

  121. i = GetDIBits(hBMPhDC, Pic1Handle, 0, H, fPic(0, 0, 0), BI1, 0)

  122. ReleaseDC 0, hBMPhDC
  123. For m = 0 To 2
  124.     RGB(m) = CLng("&H" & Mid(color, m * 2 + 1, 2))
  125. Next
  126. '分析查找
  127. For j = 0 To H - 1
  128.     For i = 0 To W - 1
  129.                 If fPic(2, i, j) <> RGB(0) Then GoTo ExitLine:   'R
  130.                 If fPic(1, i, j) <> RGB(1) Then GoTo ExitLine:   'G
  131.                 If fPic(0, i, j) <> RGB(2) Then GoTo ExitLine:   'B
  132.         FindColor = i & "," & j
  133.         Exit Function

  134. ExitLine:
  135.     Next i
  136. Next j

  137. End Function


  138. Public Function FindColorE(Left As Long, Top As Long, Right As Long, Bottom As Long, color As String, sim As Double) As String
  139. Dim l() As String
  140. Dim W As Long, H As Long, i As Long, j As Long
  141. Dim RGB(3) As Byte
  142. Dim fPic() As Byte
  143. Dim m As Long
  144. sim = (1 - (1 - sim) * 0.65)
  145. W = Right
  146. H = Bottom
  147. With BI1.bmiHeader
  148.     .biSize = Len(BI1.bmiHeader)
  149.     .biWidth = W
  150.     .biHeight = -H
  151.     .biBitCount = 32
  152.     .biPlanes = 1
  153. End With
  154. ReDim fPic(3, W - 1, H - 1)
  155.      Dim hBMPhDC
  156.      Dim hDCmem As Long
  157.      Dim Pic1Handle As Long
  158.      Dim hBmpPrev As Long
  159.      hBMPhDC = GetDC(0)
  160.      hDCmem = CreateCompatibleDC(hBMPhDC)
  161.      Pic1Handle = CreateCompatibleBitmap(hBMPhDC, Right, Bottom)
  162.      hBmpPrev = SelectObject(hDCmem, Pic1Handle)
  163.      BitBlt hDCmem, 0, 0, Right, Bottom, hBMPhDC, Left, Top, SRCCOPY
  164.      DeleteDC hDCmem

  165. i = GetDIBits(hBMPhDC, Pic1Handle, 0, H, fPic(0, 0, 0), BI1, 0)

  166. ReleaseDC 0, hBMPhDC
  167. For m = 0 To 2
  168.     RGB(m) = CLng("&H" & Mid(color, m * 2 + 1, 2))
  169. Next
  170. '分析查找
  171. For j = 0 To H - 1
  172.     For i = 0 To W - 1
  173.                 If fPic(2, i, j) < CLng(RGB(0) * sim) Or fPic(2, i, j) > CLng(RGB(0) * (2 - sim)) Then GoTo ExitLine: 'R
  174.                 If fPic(1, i, j) < CLng(RGB(1) * sim) Or fPic(1, i, j) > CLng(RGB(1) * (2 - sim)) Then GoTo ExitLine:   'G
  175.                 If fPic(0, i, j) < CLng(RGB(2) * sim) Or fPic(0, i, j) > CLng(RGB(2) * (2 - sim)) Then GoTo ExitLine:   'B
  176.         FindColorE = i & "," & j
  177.         Exit Function

  178. ExitLine:
  179.     Next i
  180. Next j

  181. End Function
复制代码

该用户从未签到

发表于 2015-4-12 05:05:48 | 显示全部楼层
vb找图找色源代码出于何处?

该用户从未签到

发表于 2015-4-12 09:37:06 | 显示全部楼层
回帖支持楼主分享精神!
  • TA的每日心情
    难过
    2023-7-23 15:23
  • 发表于 2016-2-2 23:05:35 | 显示全部楼层
    这个如果做好比按键强多了
    点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
    您需要登录后才可以回帖 登录 | 注册账号

    本版积分规则

    小黑屋|手机版|Archiver|看流星社区 |网站地图

    GMT+8, 2024-4-27 06:32

    Powered by Kanliuxing X3.4

    © 2010-2019 kanliuxing.com

    快速回复 返回顶部 返回列表