在VB下如何将一幅彩色位图文件(.bmp)转换成单色(黑白)位图文件(.bmp)。我是初学者,请高手们讲解得详细点。万分感谢!!

解决方案 »

  1.   

    Private Declare Function GetPixel Lib "gdi32" _
         (ByVal hdc As Long, ByVal x As Long, ByVal Y As Long) As Long
    Private Declare Function SetPixelV Lib "gdi32" _
         (ByVal hdc As Long, ByVal x As Long, _
          ByVal Y As Long, ByVal crColor As Long) As Long
    Private tmpPic As PicturePrivate Sub Form_Load()
        Picture1.ScaleMode = 3
        Picture1.AutoRedraw = True
        Set tmpPic = Picture1.Picture
    End SubPrivate Sub Command1_click()
        Dim width5  As Long, heigh5 As Long, rgb5 As Long
        Dim hdc5 As Long, i As Long, j As Long
        Dim bBlue As Long, bRed As Long, bGreen As Long
        Dim Y As Long
        
        width5 = Picture1.ScaleWidth
        heigh5 = Picture1.ScaleHeight
        hdc5 = Picture1.hdc
        For i = 1 To width5
            For j = 1 To heigh5
                rgb5 = GetPixel(hdc5, i, j)
                bBlue = Blue(rgb5)      '获得兰色值
                bRed = Red(rgb5)        '获得红色值
                bGreen = Green(rgb5)    '获得绿色值
                '将三原色转换为灰度
                Y = (9798 * bRed + 19235 * bGreen + 3735 * bBlue) \ 32768
                '将灰度转换为RGB
                rgb5 = RGB(Y, Y, Y)
                SetPixelV hdc5, i, j, rgb5
            Next j
        Next i
        Set Picture1.Picture = Picture1.Image
    End SubPrivate Function Red(ByVal mlColor As Long) As Long
        '从RGB值中获得红色值
        Red = mlColor And &HFF
    End Function
    Private Function Green(ByVal mlColor As Long) As Long
        '从RGB值中获得绿色值
        Green = (mlColor \ &H100) And &HFF
    End Function
    Private Function Blue(ByVal mlColor As Long) As Long
        ''从RGB值中获得蓝色值
        Blue = (mlColor \ &H10000) And &HFF
    End Function
      

  2.   

    http://expert.csdn.net/Expert/topicview.asp?id=1538596
    里面有彩色位图转为256色16色2色256级灰度的代码,代码很长,我就不贴过来了
      

  3.   

    谢谢各位!rainstormmaster(rainstormmaster) ,代码好!你对 位图信息头 了解得多吗?我想再请教一下,学习学习。