I have a function that changes the brightness of an image.
However I am getting the error "Arithmetic operation caused an overflow. You may not divide by 0.". in the line
bData.ByteData(ii) = CByte(bData.ByteData(ii) + (amount * (255 - bData.ByteData(ii)))) 'blue
"amount" is 30.
I am not sure what I did wrong. Does anybody see my error? Thank you!
Public Sub Brightness(Optional ByVal amount As Single = 0)
OnFilterStarted()
If amount = 0 Then Return
Dim bData = BitmapData.LockBits(b)
If amount > 0 Then
For ii = bData.ByteData.GetLowerBound(0) To bData.ByteData.GetUpperBound(0) Step 4
bData.ByteData(ii) = CByte(bData.ByteData(ii) + (amount * (255 - bData.ByteData(ii)))) 'blue
bData.ByteData(ii + 1) = CByte(bData.ByteData(ii + 1) + (amount * (255 - bData.ByteData(ii + 1)))) 'green
bData.ByteData(ii + 2) = CByte(bData.ByteData(ii + 2) + (amount * (255 - bData.ByteData(ii + 2)))) 'red
Next
Else
For ii = bData.ByteData.GetLowerBound(0) To bData.ByteData.GetUpperBound(0) Step 4
bData.ByteData(ii) = CByte(bData.ByteData(ii) - (Math.Abs(amount) * bData.ByteData(ii))) 'blue
bData.ByteData(ii + 1) = CByte(bData.ByteData(ii + 1) - (Math.Abs(amount) * bData.ByteData(ii + 1))) 'green
bData.ByteData(ii + 2) = CByte(bData.ByteData(ii + 2) - (Math.Abs(amount) * bData.ByteData(ii + 2))) 'red
Next
End If
bData.UnlockBits()
OnFilterFinished()
End Sub