当前位置:   article > 正文

VB6灰度化图片的方法_vb6 图形处理

vb6 图形处理

应用场景

自定义的button控件,其视觉效果是绘图实现的,其中涉及贴图、绘图、画文本,一幅图绘制完成后,如果该button被设置为Enabled=False,则需要对图进行灰度化处理。

方法1:再造各像素点

对图像中的每个像素点进行重新计算一个新值,构成一幅灰度化的图像。网上烂大街的方法就是它。

优点:简单、直接

缺点:低效,图像尺寸稍微大一点的话,慢得无法忍受,体验极差

  1. Private Sub Command1_Click()
  2. Dim i As Long, j As Long, R As Integer, G As Integer, B As Integer, newColor As Long
  3. For i = 0 To Picture1.ScaleWidth - 1
  4. For j = 0 To Picture1.ScaleHeight - 1
  5. GetRGBColors Picture1.Point(i, j), R, G, B
  6. newColor = (3 * R + 6 * G + B) / 10
  7. Picture1.PSet (i, j), RGB(newColor, newColor, newColor)
  8. Next
  9. Next
  10. End Sub
  11. '根据颜色的Long值获取颜色的16进制RGB值
  12. Private Function GetRGBColors(ByVal Color As Long, R As Integer, G As Integer, B As Integer)
  13. Dim sHexColor As String
  14. If Color = -1 Then
  15. R = 0
  16. G = 0
  17. B = 0
  18. Else
  19. sHexColor = String(6 - Len(Hex(Color)), "0") & Hex(Color)
  20. R = "&H" & Mid(sHexColor, 5, 2)
  21. G = "&H" & Mid(sHexColor, 3, 2)
  22. B = "&H" & Mid(sHexColor, 1, 2)
  23. End If
  24. End Function

方法2:用windows API

下面是快速灰度化与二值化源代码,网上找的,亲测,管用!

  1. Option Explicit
  2. Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
  3. Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
  4. 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
  5. 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
  6. Private Type BitMapInfoHeader ''文件信息头——BITMAPINFOHEADER
  7. biSize As Long
  8. biWidth As Long
  9. biHeight As Long
  10. biPlanes As Integer
  11. biBitCount As Integer
  12. biCompression As Long
  13. biSizeImage As Long
  14. biXPelsPerMeter As Long
  15. biYPelsPerMeter As Long
  16. biClrUsed As Long
  17. biClrImportant As Long
  18. End Type
  19. Private Type RGBQuad
  20. rgbBlue As Byte
  21. rgbGreen As Byte
  22. rgbRed As Byte
  23. ''rgbReserved As Byte
  24. End Type
  25. Private Type BitMapInfo
  26. bmiHeader As BitMapInfoHeader
  27. bmiColors As RGBQuad
  28. End Type
  29. Private Sub Command1_Click()
  30. Dim ix As Integer
  31. Dim iy As Integer
  32. Dim iWidth As Integer '以像素为单位的图形宽度
  33. Dim iHeight As Integer '以像素为单位的图形高度
  34. Dim bytGray As Byte
  35. Dim bytThreshold As Byte
  36. Dim bits() As Byte '三维数组,用于获取原彩色图像中各像素的RGB数值以及存放转化后的灰度值
  37. Dim bitsBW() As Byte '三维数组,用于存放转化为黑白图后各像素的值
  38. '获取图形的宽度和高度
  39. iWidth = Picture1.ScaleWidth / Screen.TwipsPerPixelX
  40. iHeight = Picture1.ScaleHeight / Screen.TwipsPerPixelY
  41. Picture1.Picture = Picture1.Image
  42. '创建并初始化一个bitMapInfo自定义类型
  43. Dim bi24BitInfo As BitMapInfo
  44. With bi24BitInfo.bmiHeader
  45. .biBitCount = 32
  46. .biCompression = 0&
  47. .biPlanes = 1
  48. .biSize = Len(bi24BitInfo.bmiHeader)
  49. .biWidth = iWidth
  50. .biHeight = Picture1.ScaleHeight / Screen.TwipsPerPixelY
  51. End With
  52. '重新定义数组大小
  53. ReDim bits(3, 0 To iWidth, 0 To iHeight) As Byte
  54. ReDim bitsBW(3, 0 To iWidth, 0 To iHeight) As Byte
  55. '使用GetDIBits方法一次性获取picture1中各点的rgb值,比point方法或getPixel函数逐像素获取像素rgb要快出一个数量级
  56. Dim lrtn As Long
  57. lrtn = GetDIBits(Picture1.hdc, Picture1.Picture.Handle, 0&, iHeight, bits(0, 0, 0), bi24BitInfo, 0&)
  58. '数组的三个维度分别代表像素的RGB分量、以图形左下角为原点的X和Y坐标。
  59. '具体说来,这时bits(0,2,3)代表从图形左下角数起横向第2个纵向第3个像素的Blue值,而bits(1,2,3)和bits(2,2,3)分别的Green值和Red值.
  60. bytThreshold = 128 '这里定义转换为黑白图像时的阈值为128,即灰色亮度大于128的像素转为白色,小于128的像素转为黑的,此值可根据需要修改为0-255之前任意数值
  61. For ix = 0 To iWidth
  62. For iy = 0 To iHeight
  63. '***********RGB转为灰度的算法有多种,这里给出常见的两种*******
  64. 'bytGray = bits(0, ix, iy) * 0.11 + bits(1, ix, iy) * 0.59 + bits(2, ix, iy) * 0.3 '这是传统的根据三原色亮度加权得到灰阶的算法
  65. bytGray = (bits(0, ix, iy) ^ 2.2 * 0.0722 + bits(1, ix, iy) ^ 2.2 * 0.7152 + bits(2, ix, iy) ^ 2.2 * 0.2126) ^ (1 / 2.2) '这是简化 sRGB IEC61966-2.1 [gamma=2.20],有点类似于photoshop中所用的算法
  66. bits(0, ix, iy) = bytGray
  67. bits(1, ix, iy) = bytGray
  68. bits(2, ix, iy) = bytGray
  69. '*********转为黑白图像********
  70. If bits(0, ix, iy) < bytThreshold Then
  71. bitsBW(0, ix, iy) = 0
  72. bitsBW(1, ix, iy) = 0
  73. bitsBW(2, ix, iy) = 0
  74. Else
  75. bitsBW(0, ix, iy) = 255
  76. bitsBW(1, ix, iy) = 255
  77. bitsBW(2, ix, iy) = 255
  78. End If
  79. Next
  80. Next
  81. '将灰度图显示到picture2中
  82. Picture2.Picture = Picture2.Image '如果picture2的picture属性为空,需要在setDIBits之前将其picture属性设置一下,否则无法显示出图形
  83. SetDIBits Picture2.hdc, Picture2.Picture.Handle, 0&, iHeight, bits(0, 0, 0), bi24BitInfo, 0&
  84. Picture2.Picture = Picture2.Image
  85. End Sub
  86. Private Sub Form_Load()
  87. Picture1.Picture = LoadPicture(App.Path & "\66668.JPG")
  88. Picture2.Width = Picture1.Width
  89. Picture2.Height = Picture1.Height
  90. End Sub

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/125197
推荐阅读
相关标签
  

闽ICP备14008679号