版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
自己胡乱想出来的几个自定义的图像颜色处理方法:
1、R、G、B中最大的
R最大返回(255, 0, 0)
G最大返回(0, 255, 0)
B最大返回(0, 0, 255)
2、R、G、B中最大的
R最大返回(R, 0, 0)
G最大返回(0, G, 0)
B最大返回(0, 0, B)
3、R、G、B中最大的
R最大返回(R, R, R)
G最大返回(G, G, G)
B最大返回(B, B, B)
用到一个自定义方法,返回R、G、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, G, 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, G, B)
Case "r"
pDestColor = Color.FromArgb(255, 0, 0)
Case "g"
pDestColor = Color.FromArgb(0, 255, 0)
Case Else
pDestColor = Color.FromArgb(0, 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, sourceImg.Height)
Dim R, G, 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, G, B)
Case "r"
pDestColor = Color.FromArgb(R, 0, 0)
Case "g"
pDestColor = Color.FromArgb(0, G, 0)
Case Else
pDestColor = Color.FromArgb(0, 0, B)
End Select
destImg.SetPixel(i, j, 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, sourceImg.Height)
Dim R, G, 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, G, B)
Case "r"
pDestColor = Color.FromArgb(R, R, R)
Case "g"
pDestColor = Color.FromArgb(G, G, G)
Case Else
pDestColor = Color.FromArgb(B, B, B)
End Select
destImg.SetPixel(i, j, pDestColor)
Next
Next
picDest.Image = destImg
End Sub
运行效果:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看 vb.net教程 目录
————————————————
版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。