Results 1 to 6 of 6

Thread: [RESOLVED] LockBits has different effect?

Threaded View

  1. #1

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

    Resolved [RESOLVED] LockBits has different effect?

    Hi, I've made an Aero-style form header (see signature) and I wanted to speed it up, so I used LockBits, and it had a different effect. These two methods should do the same thing, right? Well, it doesn't. Image 1 is before LockBits, Image 2 is after. Using LockBits gives a streaking effect that I don't like. What's happening?
    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 i As Integer
    9.         For i = 0 To radius
    10.             Dim y, x As Integer
    11.             For y = bb.Y To bb.Bottom - 1
    12.                 For x = bb.X To bb.Right - 1
    13.                     Try
    14.                         Dim px_up As Color = img.GetPixel(x, y - 1)
    15.                         Dim px_down As Color = img.GetPixel(x, y + 1)
    16.                         Dim px_left As Color = img.GetPixel(x - 1, y)
    17.                         Dim px_right As Color = img.GetPixel(x + 1, y)
    18.                         Dim pxR As Integer = Average_Int(px_up.R, px_down.R, px_left.R, px_right.R)
    19.                         Dim pxG As Integer = Average_Int(px_up.G, px_down.G, px_left.G, px_right.G)
    20.                         Dim pxB As Integer = Average_Int(px_up.B, px_down.B, px_left.B, px_right.B)
    21.                         Dim n_px As Color = Color.FromArgb(255, pxR, pxG, pxB)
    22.                         img.SetPixel(x, y, n_px)
    23.                     Catch ex As Exception
    24.                     End Try
    25.                 Next
    26.             Next
    27.         Next
    28.     End Sub
    29.     Private Sub BlurImageLockBits(ByVal img As Bitmap, Optional ByVal radius As Integer = 3, Optional ByVal blur_bounds As Object = Nothing)
    30.         Dim bb As Rectangle
    31.         If blur_bounds IsNot Nothing Then
    32.             bb = CType(blur_bounds, Rectangle)
    33.         Else
    34.             bb = New Rectangle(0, 0, img.Width, img.Height)
    35.         End If
    36.         Dim bdata As Imaging.BitmapData = img.LockBits(bb, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb)
    37.         Dim img_info(bdata.Width * bdata.Height) As Integer
    38.         System.Runtime.InteropServices.Marshal.Copy(bdata.Scan0, img_info, 0, img_info.Length)
    39.         Dim i As Integer
    40.         For i = 0 To radius
    41.             Dim y, x As Integer
    42.             For y = bb.Y To bb.Bottom - 1
    43.                 For x = bb.X To bb.Right - 1
    44.                     Try
    45.                         Dim px_up As Color = Color.FromArgb(img_info(((y - 1) * bdata.Width) + x))
    46.                         Dim px_down As Color = Color.FromArgb(img_info(((y + 1) * bdata.Width) + x))
    47.                         Dim px_left As Color = Color.FromArgb(img_info((y * bdata.Width) + x - 1))
    48.                         Dim px_right As Color = Color.FromArgb(img_info((y * bdata.Width) + x + 1))
    49.                         Dim pxR As Integer = Average_Int(px_up.R, px_down.R, px_left.R, px_right.R)
    50.                         Dim pxG As Integer = Average_Int(px_up.G, px_down.G, px_left.G, px_right.G)
    51.                         Dim pxB As Integer = Average_Int(px_up.B, px_down.B, px_left.B, px_right.B)
    52.                         Dim n_px As Color = Color.FromArgb(255, pxR, pxG, pxB)
    53.                         img_info((y * bdata.Width) + x) = n_px.ToArgb()
    54.                     Catch ex As Exception
    55.                     End Try
    56.                 Next
    57.             Next
    58.         Next
    59.         System.Runtime.InteropServices.Marshal.Copy(img_info, 0, bdata.Scan0, img_info.Length)
    60.         img.UnlockBits(bdata)
    61.     End Sub
    Attached Images Attached Images   

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