vb.net 改变图像的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19202188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing color of an image
提问by Shahul hameed
I m making an windows application using vb.net 2008,in that i want to change color of an image programmatically. For e.g. Change white color to green. Can you please help me. Thanks in advance.
我正在使用 vb.net 2008 制作一个 Windows 应用程序,因为我想以编程方式更改图像的颜色。例如,将白色更改为绿色。你能帮我么。提前致谢。
回答by Synaps3
Here is the basic idea, you can modify for your needs. Create a button and a pic box named pic. Place this code in the button click event and replace path with an image of your choice. You just need to alter code in the if statements to create thresholds of certain colors. To figure out the RGB values for your thresholds, you can probably use a paint program or online color thing to find the right values for the color range you want.
这是基本思想,您可以根据需要进行修改。创建一个按钮和一个名为 pic 的 pic 框。将此代码放在按钮单击事件中,并用您选择的图像替换路径。您只需要更改 if 语句中的代码即可创建某些颜色的阈值。要计算阈值的 RGB 值,您可能可以使用绘图程序或在线颜色工具为您想要的颜色范围找到正确的值。
Dim x As Integer
Dim y As Integer
Dim red As Byte
Dim green As Byte
Dim blue As Byte
Dim img As Bitmap = New Bitmap("D:\dump\raycaster\Debug\pics\redbrick.png")
For x = 0 To img.Width - 1
For y = 0 To img.Height - 1
red = img.GetPixel(x, y).R
green = img.GetPixel(x, y).G
blue = img.GetPixel(x, y).B
If red > 128 Then
img.SetPixel(x, y, Color.Green)
End If
If blue > 200 And green > 200 Then
img.SetPixel(x, y, Color.Red)
End If
Next
Next
pic.Image = img
And for specifically changing white to green like you said:
并且像您说的那样专门将白色更改为绿色:
If red > 240 And green > 240 And blue > 240 Then
img.SetPixel(x, y, Color.Green)
End If
I did 240 because a lot of times you're not going to have a pure white.
我做了 240 次,因为很多时候你不会得到纯白色。

