图形过滤器代码中的IndexOutOfRangeException

时间:2014-05-03 20:34:03

标签: .net vb.net math

我有一些压缩位图的代码。 但是在这段代码的某处,我有一个导致IndexOutOfRangeException异常的缺陷。 我无法发现我哪里出错了。

有人看到我做错了吗?

我目前的代码是:

    Public Sub Emboss()
        OnFilterStarted()
        Dim bData = BitmapData.LockBits(b)

        Dim iByteDataLower As Integer = bData.ByteData.GetLowerBound(0)
        Dim iByteDataUpper As Integer = bData.ByteData.Length
        Dim iBmpWidth As Integer = bData.Bitmap.Width
        Dim iBmpHeight As Integer = bData.Bitmap.Height

        Const Level1 As Byte = 127
        Const Level2 As Byte = 255
        For ii = bData.ByteData.GetLowerBound(0) To bData.ByteData.Length - 1 Step 4
            If (ii + 4) Mod (bData.Bitmap.Width * 4) = 0 Then
                'right line - make gray
                bData.ByteData(ii) = Level1 'b
                bData.ByteData(ii + 1) = Level1 'g
                bData.ByteData(ii + 2) = Level1 'r
                Continue For
            End If
            Dim iY = bData.YFromOffset(ii)
            If iY = bData.Bitmap.Height - 1 Then
                'bottom line - make gray
                bData.ByteData(ii) = Level1 'b
                bData.ByteData(ii + 1) = Level1 'g
                bData.ByteData(ii + 2) = Level1 'r
                Continue For
            End If

            Dim OffsetPixelPointer = ii + ((bData.Bitmap.Width + 1) * 4)

            bData.ByteData(ii) = CByte(Math.Min(Math.Abs(CInt(bData.ByteData(ii)) - CInt(bData.ByteData(OffsetPixelPointer))) + Level1, Level2)) 'b
            bData.ByteData(ii + 1) = CByte(Math.Min(Math.Abs(CInt(bData.ByteData(ii + 1)) - CInt(bData.ByteData(OffsetPixelPointer + 1))) + Level1, Level2)) 'g
            bData.ByteData(ii + 2) = CByte(Math.Min(Math.Abs(CInt(bData.ByteData(ii + 2)) - CInt(bData.ByteData(OffsetPixelPointer + 2))) + Level1, Level2)) 'r
            'bData.ByteData(ii + 3) = 255 'a

        Next

        bData.UnlockBits()
        OnFilterFinished()
    End Sub

错误发生在

行中
bData.ByteData(ii) = CByte(Math.Min(Math.Abs(CInt(bData.ByteData(ii)) - CInt(bData.ByteData(OffsetPixelPointer))) + Level1, Level2)) 'b

bData.ByteData.Length是65536 OffsetPixelPointer是65796

很明显,这不起作用。 OffsetPixelPointer超过bData.ByteData.Length,但我不确定这是怎么发生的。

我一次又一次地查看了我的代码,但我无法发现错误。 我在某个地方犯了错误吗?

0 个答案:

没有答案