VB实例编程:VB6把彩色图片变成灰阶的方法_第2页

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2011年11月10日

  ’旋转
  For Y = 1 To tSBmpInfo.bmHeight
  For x = 1 To tSBmpInfo.bmWidth
  b = (x - 1) * BytesPixel + 1
  l = sBits(b, Y) * 0.114 + sBits(b + 1, Y) * 0.587 + sBits(b + 2, Y) * 0.299
  dBits(b, Y) = l
  dBits(b + 1, Y) = l
  dBits(b + 2, Y) = l
  ’l = dBits((x - 1) * BytesPixel + 1, Y) * 0.114 + dBits((x - 1) * BytesPixel + 2, Y) * 0.587 + dBits((x - 1) * BytesPixel + 3, Y) * 0.299
  ’dBits((x - 1) * BytesPixel + 1, Y) = l
  ’dBits((x - 1) * BytesPixel + 2, Y) = l
  ’dBits((x - 1) * BytesPixel + 3, Y) = l
  Next x
  Next Y
  Call SetBitmapBits(hDestBmp, tDBmpInfo.bmWidthBytes * tDBmpInfo.bmHeight, dBits(1, 1))
  End Function
  ’算法2.2 提高速度约2秒 是全国计算机等级考试网,加入收藏方法1的46倍速度 参数不一样
  Public Function TurnPicGray(hSrcBmp As Long) As Boolean
  Dim x As Long, Y As Long
  Dim BytesPixel As Long
  Dim tSBmpInfo As BITMAP
  Dim sBits() As Byte
  ’获得位图信息
  Call GetObject(hSrcBmp, Len(tSBmpInfo), tSBmpInfo)
  ’申请空间
  ReDim sBits(1 To tSBmpInfo.bmWidthBytes, 1 To tSBmpInfo.bmHeight)
  ’获得源图与目标图二进制位
  Call GetBitmapBits(hSrcBmp, tSBmpInfo.bmWidthBytes * tSBmpInfo.bmHeight, sBits(1, 1))
  ’计算颜色值占用多少字节
  BytesPixel = tSBmpInfo.bmBitsPixel / 8
  Dim l As Integer
  Dim b As Long
  ’旋转
  For Y = 1 To tSBmpInfo.bmHeight
  For x = 1 To tSBmpInfo.bmWidth
  b = (x - 1) * BytesPixel + 1
  l = sBits(b, Y) * 0.114 + sBits(b + 1, Y) * 0.587 + sBits(b + 2, Y) * 0.299
  sBits(b, Y) = l
  sBits(b + 1, Y) = l
  sBits(b + 2, Y) = l
  Next x
  Next Y
  Call SetBitmapBits(hSrcBmp, tSBmpInfo.bmWidthBytes * tSBmpInfo.bmHeight, sBits(1, 1))
  End Function
  ’算法
  Public Sub SetPicGray(Pic As PictureBox)
  Dim width5 As Long, heigh5 As Long, rgb5 As Long
  Dim hdc5 As Long, i As Long, j As Long


首页 1 2 尾页

相关文章