|
-
Jan 13th, 2010, 10:55 PM
#1
Thread Starter
Stack Overflow moderator
[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:
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 i As Integer
For i = 0 To radius
Dim y, x As Integer
For y = bb.Y To bb.Bottom - 1
For x = bb.X 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)
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.SetPixel(x, y, n_px)
Catch ex As Exception
End Try
Next
Next
Next
End Sub
Private Sub BlurImageLockBits(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 bdata As Imaging.BitmapData = img.LockBits(bb, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppArgb)
Dim img_info(bdata.Width * bdata.Height) As Integer
System.Runtime.InteropServices.Marshal.Copy(bdata.Scan0, img_info, 0, img_info.Length)
Dim i As Integer
For i = 0 To radius
Dim y, x As Integer
For y = bb.Y To bb.Bottom - 1
For x = bb.X To bb.Right - 1
Try
Dim px_up As Color = Color.FromArgb(img_info(((y - 1) * bdata.Width) + x))
Dim px_down As Color = Color.FromArgb(img_info(((y + 1) * bdata.Width) + x))
Dim px_left As Color = Color.FromArgb(img_info((y * bdata.Width) + x - 1))
Dim px_right As Color = Color.FromArgb(img_info((y * bdata.Width) + x + 1))
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_info((y * bdata.Width) + x) = n_px.ToArgb()
Catch ex As Exception
End Try
Next
Next
Next
System.Runtime.InteropServices.Marshal.Copy(img_info, 0, bdata.Scan0, img_info.Length)
img.UnlockBits(bdata)
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|