前三行是分解RGB分量
不过效率较低red = pixel And &HFF
green = (pixel And &HFF00&) \ &H100
blue = (pixel And &HFF0000) \ &H10000
第四句是计算灰度
只不过化简了标准的是:gs = red * 0.299 + green * 588 + blue * 0.113这样写效率高些:gs = (red * 30 + green * 59 + blue * 11) \ 100

解决方案 »

  1.   

    为什么这样写呀:
    red = pixel And &HFF
    green = (pixel And &HFF00&) \ &H100
    blue = (pixel And &HFF0000) \ &H10000
      

  2.   

    通过位运算分离RGB分量你计算机原理没学好吧
      

  3.   

    我也一直都只用这个办法来分RGB色,但总觉得很慢。。
    我有个想法,是否可以用字符串的方法来分呢?那样速度一定很快。。
    初步写了这三个涵数。但有些时候要出错的很麻烦。。
    谁能写一个完善点的?^_^Private Function fenRed(theColor As Long) As Long
        Dim myColor As String
        myColor = Hex(theColor)
        Debug.Print "fenGreen:" & myColor
        myColor = Right(myColor, 2)
        fenRed = "&H" & myColor
    End FunctionPrivate Function fenGreen(theColor As Long) As Long
        Dim myColor As String
        myColor = Hex(theColor)
        Debug.Print "fenGreen:" & myColor
        If Len(myColor) = 6 Then
                myColor = Mid(myColor, 3, 2)
            Else
                myColor = Mid(myColor, 2, 2)
        End If
        fenGreen = "&H" & myColor
    End FunctionPrivate Function fenBlue(theColor As Long) As Long
        Dim myColor As String
        myColor = Hex(theColor)
        Debug.Print "fenBlue:" & myColor
        If Len(myColor) = 6 Then
                myColor = Right(myColor, 2)
            Else
                myColor = Right(myColor, 1)
        End If
        fenBlue = "&H" & myColor
    End Function
      

  4.   

    字符串运算更慢直接对内存中的DIB位图数据进行处理
    快30倍以上再加上模拟指针操作内存
    快120倍以上(注意,不是笔误,真的没有%)
      

  5.   

    and是位与
    \&H100相当于右移8位
    \&H10000相当于右移16位
      

  6.   

    数据类型要事先定义(否则为Variant)
    这样速度快些Private Sub cmdGray_Click()
        Dim x as long, y as long
        Dim I as long, J as long
        Dim Pixel as long
        Dim Red as long, Green as long, Blue as long
        Dim gs as long
        
        Pic.ScaleMode = vbPixels
        x = Pic.ScaleWidth
        y = Pic.ScaleHeight
        For i = 0 To y-1
            For j = 0 To x-1
                pixel = Pic.Point(j, i)
                red = pixel And &HFF
                green = (pixel And &HFF00&) \ &H100
                blue = (pixel And &HFF0000) \ &H10000
                gs = (red * 30 + green * 59 + blue * 11) \ 100
                Pic.PSet (j, i), RGB(gs, gs, gs)
            Next j
        Next i
        Pic.ScaleMode = vbTwips
        
    End Sub
      

  7.   

    原来还
    red = pixel And &HFF
    green = (pixel And &HFF00&) \ &H100
    blue = (pixel And &HFF0000) \ &H10000
    快这么多的呀!??
    哎~~我自作聪明了……谢谢zyl910(910:分儿,我来了!) !!
      

  8.   

    to zyl910(910:分儿,我来了!)
    我发现你懂的很多呀,你是怎样学习的!!!!1