Results 1 to 4 of 4

Thread: Blur with LockBits not working?

  1. #1

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Blur with LockBits not working?

    I tried using LockBits to blur images, since I heard it was faster, and there's no error, but there's no blur either. It doesn't do anything. The commented-out code is the code I had before using LockBits, and it works.
    vb.net Code:
    1. Private Sub BlurImage(ByVal img As Bitmap, Optional ByVal radius As Integer = 3, Optional ByVal blur_bounds As Object = Nothing)
    2.         Dim bb As Rectangle
    3.         If blur_bounds IsNot Nothing Then
    4.             bb = CType(blur_bounds, Rectangle)
    5.         Else
    6.             bb = New Rectangle(0, 0, img.Width, img.Height)
    7.         End If
    8.         Dim b_data As Drawing.Imaging.BitmapData = img.LockBits(bb, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb)
    9.         Dim ptr As IntPtr = b_data.Scan0
    10.         Dim bytes As Integer = b_data.Stride * b_data.Height
    11.         Dim img_bytes(bytes - 1) As Byte
    12.         System.Runtime.InteropServices.Marshal.Copy(ptr, img_bytes, 0, bytes)
    13.         Dim i As Integer
    14.         For i = 0 To radius
    15.             Dim y, x As Integer
    16.             For y = (bb.Y + 1) To bb.Bottom - 1
    17.                 For x = (bb.X + 1) To bb.Right - 1
    18.                     Try
    19.                         'Dim px_up As Color = img.GetPixel(x, y - 1)
    20.                         'Dim px_down As Color = img.GetPixel(x, y + 1)
    21.                         'Dim px_left As Color = img.GetPixel(x - 1, y)
    22.                         'Dim px_right As Color = img.GetPixel(x + 1, y)
    23.                         'Dim pxR As Integer = Average_Int(px_up.R, px_down.R, px_left.R, px_right.R) ', 90)
    24.                         'Dim pxG As Integer = Average_Int(px_up.G, px_down.G, px_left.G, px_right.G) ', 90)
    25.                         'Dim pxB As Integer = Average_Int(px_up.B, px_down.B, px_left.B, px_right.B) ', 90) '90s are added for "blackness"
    26.                         'Dim n_px As Color = Color.FromArgb(255, pxR, pxG, pxB)
    27.                         'img.SetPixel(x, y, n_px)
    28.                         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))
    29.                         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))
    30.                         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))
    31.                         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))
    32.                         Dim pxR As Integer = Average_Int(px_up.R, px_down.R, px_left.R, px_right.R)
    33.                         Dim pxG As Integer = Average_Int(px_up.G, px_down.G, px_left.G, px_right.G)
    34.                         Dim pxB As Integer = Average_Int(px_up.B, px_down.B, px_left.B, px_right.B)
    35.                         Dim n_px As Color = Color.FromArgb(255, pxR, pxG, pxB)
    36.                         img_bytes((y * bb.Width + x) * 4) = n_px.A
    37.                         img_bytes((y * bb.Width + x) * 4 + 1) = n_px.R
    38.                         img_bytes((y * bb.Width + x) * 4 + 2) = n_px.G
    39.                         img_bytes((y * bb.Width + x) * 4 + 3) = n_px.B
    40.                     Catch ex As Exception
    41.                         GoTo finish_sub
    42.                     End Try
    43.                 Next
    44.             Next
    45.         Next
    46. finish_sub:
    47.         img.UnlockBits(b_data)
    48.     End Sub

  2. #2

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Blur with LockBits not working?

    Oh, and I also did a test like this:
    vb.net Code:
    1. Public Class Form1
    2.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    3.         Dim g As Graphics = e.Graphics
    4.         Dim img As Bitmap = My.Resources.test.Clone()
    5.         LockBitsTest(img)
    6.         g.DrawImage(img, 10, 10)
    7.         MyBase.OnPaint(e)
    8.     End Sub
    9.     Private Sub LockBitsTest(ByVal img As Bitmap)
    10.         Dim b_data As Drawing.Imaging.BitmapData = img.LockBits(New Rectangle(0, 0, img.Width, img.Height), Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb)
    11.         Dim ptr As IntPtr = b_data.Scan0
    12.         Dim bytes As Integer = b_data.Stride * b_data.Height
    13.         Dim img_bytes(bytes - 1) As Byte
    14.         System.Runtime.InteropServices.Marshal.Copy(ptr, img_bytes, 0, bytes)
    15.         Dim y, x As Integer
    16.         For y = 1 To img.Height - 2
    17.             For x = 1 To img.Width - 2
    18.                 Try
    19.                     img_bytes((y * img.Width + x) * 4) = 255
    20.                     img_bytes((y * img.Width + x) * 4 + 1) = 255
    21.                     'img_bytes((y * img.Width + x) * 4 + 2) = n_px.G
    22.                     'img_bytes((y * img.Width + x) * 4 + 3) = n_px.B
    23.                 Catch ex As Exception
    24.                     GoTo finish_sub
    25.                 End Try
    26.             Next
    27.         Next
    28. finish_sub:
    29.         img.UnlockBits(b_data)
    30.     End Sub
    31. End Class
    It gave me no errors, but the image didn't change.

    What's wrong?

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Blur with LockBits not working?

    Pretty in depth example here. Blurring should be in the convolution filters section.

    Image Processing / Lockbits

  4. #4

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Blur with LockBits not working?

    Ok, so Int32s instead. I'll try it. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width