1、R、G、B中最大的
R最大返回(255,0)
G最大返回(0,255,0)
B最大返回(0,255)
2、R、G、B中最大的
R最大返回(R,G,B)
3、R、G、B中最大的
R最大返回(R,R,R)
G最大返回(G,G)
B最大返回(B,B,B)
Private Function getMax(ByVal r As Integer,ByVal g As Integer,ByVal b As Integer) As String If r > g And r > b Then Return "r" If g > r And g > b Then Return "g" If b > r And b > g Then Return "b" Return "b" End Function
主要代码:
'自定义1 Private Sub btnCustom1_Click(sender As Object,e As EventArgs) Handles btnCustom1.Click Dim pSourceColor As Color Dim pDestColor As Color Dim destImg As New Bitmap(sourceImg.Width,sourceImg.Height) Dim R,B As Integer For i As Integer = 0 To sourceImg.Width - 1 For j As Integer = 0 To sourceImg.Height - 1 pSourceColor = sourceImg.GetPixel(i,j) R = pSourceColor.R G = pSourceColor.G B = pSourceColor.B Select Case getMax(R,B) Case "r" pDestColor = Color.FromArgb(255,0) Case "g" pDestColor = Color.FromArgb(0,0) Case Else pDestColor = Color.FromArgb(0,255) End Select destImg.SetPixel(i,j,pDestColor) Next Next picDest.Image = destImg End Sub '自定义2 Private Sub btnCustom2_Click(sender As Object,e As EventArgs) Handles btnCustom2.Click Dim pSourceColor As Color Dim pDestColor As Color Dim destImg As New Bitmap(sourceImg.Width,B) Case "r" pDestColor = Color.FromArgb(R,B) End Select destImg.SetPixel(i,pDestColor) Next Next picDest.Image = destImg End Sub '自定义3 Private Sub btnCustom3_Click(sender As Object,e As EventArgs) Handles btnCustom3.Click Dim pSourceColor As Color Dim pDestColor As Color Dim destImg As New Bitmap(sourceImg.Width,R) Case "g" pDestColor = Color.FromArgb(G,G) Case Else pDestColor = Color.FromArgb(B,pDestColor) Next Next picDest.Image = destImg End Sub
运行效果:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看 vb.net 教程 目录