此代码始终通过算术溢出异常。怎么了?
Function ChannelSum(ByVal C As System.Drawing.Color) As Integer
Dim temp As Integer : temp = (C.R + C.G + C.B)
Return temp
End Function
...
Dim x, y, R, G, B, a As Integer : Dim tmp As Color
bmp = New Bitmap(picBox.Tag.ToString)
xMax = bmp.Width - 1 : yMax = bmp.Height - 1
For x = 0 To xMax Step 1
For y = 0 To yMax Step 1
tmp = bmp.GetPixel(x, y) : a = ChannelSum(tmp)
Next y
Next x
第一次遭遇时循环中断了!
答案 0 :(得分:3)
C.R和其他是字节字段,只能保存最多255个值。一起添加字节字段将导致大于255的数字。首先在每个颜色元素上使用CInt()。
temp = (CInt(C.R) + CInt(C.G) + CInt(C.B))