Private Sub BlurImage(ByVal img As Bitmap, Optional ByVal radius As Integer = 3, Optional ByVal blur_bounds As Object = Nothing)
Dim bb As Rectangle
If blur_bounds IsNot Nothing Then
bb = CType(blur_bounds, Rectangle)
Else
bb = New Rectangle(0, 0, img.Width, img.Height)
End If
Dim b_data As Drawing.Imaging.BitmapData = img.LockBits(bb, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb)
Dim ptr As IntPtr = b_data.Scan0
Dim bytes As Integer = b_data.Stride * b_data.Height
Dim img_bytes(bytes - 1) As Byte
System.Runtime.InteropServices.Marshal.Copy(ptr, img_bytes, 0, bytes)
Dim i As Integer
For i = 0 To radius
Dim y, x As Integer
For y = (bb.Y + 1) To bb.Bottom - 1
For x = (bb.X + 1) To bb.Right - 1
Try
'Dim px_up As Color = img.GetPixel(x, y - 1)
'Dim px_down As Color = img.GetPixel(x, y + 1)
'Dim px_left As Color = img.GetPixel(x - 1, y)
'Dim px_right As Color = img.GetPixel(x + 1, y)
'Dim pxR As Integer = Average_Int(px_up.R, px_down.R, px_left.R, px_right.R) ', 90)
'Dim pxG As Integer = Average_Int(px_up.G, px_down.G, px_left.G, px_right.G) ', 90)
'Dim pxB As Integer = Average_Int(px_up.B, px_down.B, px_left.B, px_right.B) ', 90) '90s are added for "blackness"
'Dim n_px As Color = Color.FromArgb(255, pxR, pxG, pxB)
'img.SetPixel(x, y, n_px)
Dim px_up As Color = Color.FromArgb(img_bytes(((y - 1) * bb.Width + x) * 4), img_bytes(((y - 1) * bb.Width + x) * 4 + 1), img_bytes(((y - 1) * bb.Width + x) * 4 + 2), img_bytes(((y - 1) * bb.Width + x) * 4 + 3))
Dim px_down As Color = Color.FromArgb(img_bytes(((y + 1) * bb.Width + x) * 4), img_bytes(((y + 1) * bb.Width + x) * 4 + 1), img_bytes(((y + 1) * bb.Width + x) * 4 + 2), img_bytes(((y + 1) * bb.Width + x) * 4 + 3))
Dim px_left As Color = Color.FromArgb(img_bytes((y * bb.Width + (x - 1)) * 4), img_bytes((y * bb.Width + (x - 1)) * 4 + 1), img_bytes((y * bb.Width + (x - 1)) * 4 + 2), img_bytes((y * bb.Width + (x - 1)) * 4 + 3))
Dim px_right As Color = Color.FromArgb(img_bytes((y * bb.Width + (x + 1)) * 4), img_bytes((y * bb.Width + (x + 1)) * 4 + 1), img_bytes((y * bb.Width + (x + 1)) * 4 + 2), img_bytes((y * bb.Width + (x + 1)) * 4 + 3))
Dim pxR As Integer = Average_Int(px_up.R, px_down.R, px_left.R, px_right.R)
Dim pxG As Integer = Average_Int(px_up.G, px_down.G, px_left.G, px_right.G)
Dim pxB As Integer = Average_Int(px_up.B, px_down.B, px_left.B, px_right.B)
Dim n_px As Color = Color.FromArgb(255, pxR, pxG, pxB)
img_bytes((y * bb.Width + x) * 4) = n_px.A
img_bytes((y * bb.Width + x) * 4 + 1) = n_px.R
img_bytes((y * bb.Width + x) * 4 + 2) = n_px.G
img_bytes((y * bb.Width + x) * 4 + 3) = n_px.B
Catch ex As Exception
GoTo finish_sub
End Try
Next
Next
Next
finish_sub:
img.UnlockBits(b_data)
End Sub