• 按键公众号 :
按键精灵电脑版
立即下载

软件版本:2014.06
软件大小:22.9M
更新时间:2021-12-03

按键精灵安卓版
立即下载

软件版本:3.7.2
软件大小:46.2M
更新时间:2023-05-10

按键精灵iOS版
立即下载

软件版本:1.8.0
软件大小:29.2M
更新时间:2023-03-21

按键手机助手
立即下载

软件版本:3.8.0
软件大小:262M
更新时间:2023-05-30

快捷导航

登录 后使用快捷导航
没有帐号? 注册

发新话题 回复该主题

【VB源码】图像处理之像素的获取和输出 [复制链接]

1#
'获取像素 DibGet()
'图像输出 DIBPut()
'Copy 数组 CopyData()

'用于存放从DIB输入的像素值(获取的像素数组) ColVal()
'注 第一维: 0= B 值 1= G 值 2= R 值 3= Alpha
' 第二维: X 值
' 第三维: Y 值
  1. '删除一个DC
  2. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
  3. '删除一个对象
  4. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  5. '选择当前对象
  6. Private Declare Function GetCurrentObject Lib "gdi32" (ByVal hdc As Long, ByVal uObjectType As Long) As Long
  7. '获取DIB
  8. 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
  9. '获取系统时间
  10. Private Declare Function timeGetTime Lib "winmm.dll" () As Long
  11. '输出图像
  12. Private Declare Function SetDIBitsToDevice Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal Scan As Long, ByVal NumScans As Long, Bits As Any, BitsInfo As BitMapInfo, ByVal wUsage As Long) As Long
  13. '内存操作
  14. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)

  15. '数据结构定义:
  16. Private Type BitMapInfoHeader '文件信息头——BITMAPINFOHEADER
  17. biSize As Long
  18. biWidth As Long
  19. biHeight As Long
  20. biPlanes As Integer
  21. biBitCount As Integer
  22. biCompression As Long
  23. biSizeImage As Long
  24. biXPelsPerMeter As Long
  25. biYPelsPerMeter As Long
  26. biClrUsed As Long
  27. biClrImportant As Long
  28. End Type

  29. Private Type RGBQuad
  30. rgbBlue As Byte
  31. rgbGreen As Byte
  32. rgbRed As Byte
  33. 'rgbReserved As Byte
  34. End Type

  35. Private Type BitMapInfo
  36. bmiHeader As BitMapInfoHeader
  37. bmiColors As RGBQuad
  38. End Type

  39. '过程中用到的全局变量:
  40. Private Const Bits As Long = 32 '颜色深度,这里把所有图像都按照32位来处理
  41. Public Done As Boolean '用于标记一个过程是否结束
  42. Public TimeGet As Long '用于记录输入过程处理所花费的时间
  43. Public TimePut As Long '用于记录输出过程处理所花费的时间
  44. Dim ColVal() As Byte '用于存放从DIB输入的像素值
  45. Dim ColOut() As Byte '用于存放向DIB输出的像素值
  46. Dim InPutHei As Long '用于记录输入图像的高度
  47. Dim InPutWid As Long '用于记录输入图像的宽度
  48. Dim bi24BitInfo As BitMapInfo '定义BMP信息

  49. '获取像素
  50. Public Sub DibGet(ByVal IdSource As Long, XBegin As Long, ByVal YBegin As Long, ByVal XEnd As Long, ByVal YEnd As Long)
  51. Dim iBitmap As Long
  52. Dim iDC As Long
  53. Dim I As Long
  54. Dim W As Long
  55. Dim H As Long

  56. On Error GoTo ErrLine
  57. Done = False
  58. TimeGet = timeGetTime
  59. InPutWid = XEnd - XBegin
  60. InPutHei = YEnd - YBegin

  61. W = InPutWid + 1
  62. H = InPutHei + 1

  63. I = (Bits \ 8) - 1
  64. ReDim ColVal(I, InPutWid, InPutHei)
  65. With bi24BitInfo.bmiHeader
  66. .biBitCount = Bits
  67. .biCompression = 0&
  68. .biPlanes = 1
  69. .biSize = Len(bi24BitInfo.bmiHeader)
  70. .biWidth = W
  71. .biHeight = H
  72. End With

  73. iBitmap = GetCurrentObject(IdSource, 7&)
  74. GetDIBits IdSource, iBitmap, 0&, H, ColVal(0, 0, 0), bi24BitInfo, 0&
  75. DeleteObject iBitmap
  76. Done = True
  77. TimeGet = timeGetTime - TimeGet
  78. Exit Sub
  79. ErrLine:
  80. MsgBox "错误号:" & Err.Number & ":" & Err.Description
  81. End Sub

  82. '图像输出
  83. Private Sub DIBPut(ByVal IdDestination As Long, Width As Long, Height As Long)
  84. Dim W As Long
  85. Dim H As Long
  86. Dim LineBytes As Long

  87. On Error GoTo ErrLine
  88. Done = False
  89. TimePut = timeGetTime

  90. W = Width + 1
  91. H = Height + 1

  92. With bi24BitInfo.bmiHeader
  93. .biWidth = W
  94. .biHeight = H
  95. LineBytes = ((W * Bits + 31) And &HFFFFFFE0) \ 8
  96. .biSizeImage = LineBytes * H
  97. End With
  98. SetDIBitsToDevice IdDestination, 0, 0, W, H, 0, 0, 0, H, ColOut(0, 0, 0), bi24BitInfo, 0

  99. Done = True
  100. TimePut = timeGetTime - TimePut
  101. Exit Sub
  102. On Error GoTo 0
  103. ErrLine:
  104. MsgBox Err.Description
  105. End Sub

  106. 'Copy 数组
  107. Public Sub CopyData(ByVal W As Long, ByVal H As Long)
  108. Dim Length As Long
  109. Dim I As Long
  110. Dim L As Long

  111. I = Bits \ 8
  112. L = I - 1
  113. Length = (W + 1&) * (H + 1&) * I
  114. ReDim ColOut(L, W, H)
  115. CopyMemory ColOut(0, 0, 0), ColVal(0, 0, 0), Length
  116. End Sub
复制代码
'新建一窗体 添加 Picture1 , Picture2 ,Command1
'给 Picture1 Load 一图片

'按钮事件
  1. Private Sub Command1_Click()
  2. With Picture1
  3. .AutoRedraw = True
  4. .AutoSize = True
  5. .ScaleMode = 3
  6. .BorderStyle = 0
  7. DibGet .hdc, 0, 0, .ScaleWidth, .ScaleHeight
  8. End With
  9. CopyData InPutHei, InPutWid

  10. With Picture2
  11. .AutoRedraw = True
  12. .AutoSize = True
  13. .ScaleMode = 3
  14. .BorderStyle = 0
  15. DIBPut .hdc, InPutWid, InPutHei
  16. .Refresh
  17. End With
  18. End Sub
复制代码

    已有1评分我要评分查看所有评分

    2#


    3#

    顶.................学习

    4#

    留着

    5#

    4444444444444444444444444444

    6#


    7#


    8#


    9#

    安防

    10#

    看看

    11#

    鲁迅

    12#

    好文章一定要回复的。

    13#

    学习学习~~~~~~~~~~~~~~~~~~~~~~~~

    14#

    给 Picture1 Load 一图片

    15#

    ding ding ding

    16#

    Picture1 Load 一图片

    17#

    很不错的信息,一定支持

    18#


    19#

    学习学习,多谢

    20#

    谢谢

    发新话题 回复该主题