Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Private Sub GrayscaleToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GrayscaleToolStripMenuItem.Click
        Dim bm As New Bitmap(PictureBox2.Image)
        Dim X As Integer
        Dim Y As Integer
        Dim clr As Integer

        For X = 0 To bm.Width - 1
            For Y = 0 To bm.Height - 1
                clr = (CInt(bm.GetPixel(X, Y).R) + _
                       bm.GetPixel(X, Y).G + _
                       bm.GetPixel(X, Y).B) \ 3
                bm.SetPixel(X, Y, Color.FromArgb(clr, clr, clr))
            Next Y
        Next X

        PictureBox2.Image = bm
    End Sub
    Private Function Average(ByVal Size As Size, ByVal imageSize As SizeF, ByVal PixelX As Integer, ByVal Pixely As Integer) As Color
        Dim pixels As New ArrayList
        Dim x As Integer, y As Integer
        Dim bmp As Bitmap = PictureBox2.Image.Clone

            For x = PixelX - CInt(Size.Width / 2) To PixelX + CInt(Size.Width / 2)
            For y = Pixely - CInt(Size.Height / 2) To Pixely + CInt(Size.Height / 2)
                If (x > 0 And x < imageSize.Width) And _
                   (y > 0 And y < imageSize.Height) Then
                    pixels.Add(bmp.GetPixel(x, y))
                End If
            Next
        Next

          Dim thisColor As Color

        Dim alpha As Integer = 0
        Dim red As Integer = 0
        Dim green As Integer = 0
        Dim blue As Integer = 0

        For Each thisColor In pixels
            alpha += thisColor.A
            red += thisColor.R
            green += thisColor.G
            blue += thisColor.B
        Next

        Return Color.FromArgb(alpha / pixels.Count, _
                                red / pixels.Count, _
                             green / pixels.Count, _
                             blue / pixels.Count)
    End Function

    Private Sub gausianBlur(ByVal alphaEdgesOnly As Boolean, ByVal blurSize As Size)
        Dim PixelY As Integer
        Dim PixelX As Integer
        Dim bmp As Bitmap = PictureBox2.Image.Clone

        For PixelY = 0 To bmp.Width - 1

            For PixelX = 0 To bmp.Height - 1
                If Not alphaEdgesOnly Then
                    bmp.SetPixel(PixelX, PixelY, Average(blurSize, bmp.PhysicalDimension, PixelX, PixelY))
                ElseIf bmp.GetPixel(PixelX, PixelY).A <> 255 Then
                    bmp.SetPixel(PixelX, PixelY, Average(blurSize, bmp.PhysicalDimension, PixelX, PixelY))
                End If

            Next
        Next

        PictureBox2.Image = bmp.Clone
        bmp.Dispose()
    End Sub
Posted

1 solution

Maybe PixelX or PixelY is greater than the physical dimension it's supposed to represent. Use the debugger to examine the variables BEFORE that line of code is executed.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900