|
-
Dec 23rd, 2009, 08:56 PM
#1
Thread Starter
Stack Overflow moderator
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:
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
-
Dec 23rd, 2009, 09:04 PM
#2
Thread Starter
Stack Overflow moderator
Re: Blur with LockBits not working?
Oh, and I also did a test like this:
vb.net Code:
Public Class Form1 Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) Dim g As Graphics = e.Graphics Dim img As Bitmap = My.Resources.test.Clone() LockBitsTest(img) g.DrawImage(img, 10, 10) MyBase.OnPaint(e) End Sub Private Sub LockBitsTest(ByVal img As Bitmap) Dim b_data As Drawing.Imaging.BitmapData = img.LockBits(New Rectangle(0, 0, img.Width, img.Height), 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 y, x As Integer For y = 1 To img.Height - 2 For x = 1 To img.Width - 2 Try img_bytes((y * img.Width + x) * 4) = 255 img_bytes((y * img.Width + x) * 4 + 1) = 255 'img_bytes((y * img.Width + x) * 4 + 2) = n_px.G 'img_bytes((y * img.Width + x) * 4 + 3) = n_px.B Catch ex As Exception GoTo finish_sub End Try Next Next finish_sub: img.UnlockBits(b_data) End Sub End Class
It gave me no errors, but the image didn't change.
What's wrong?
-
Dec 24th, 2009, 10:50 AM
#3
Re: Blur with LockBits not working?
Pretty in depth example here. Blurring should be in the convolution filters section.
Image Processing / Lockbits
-
Dec 24th, 2009, 02:05 PM
#4
Thread Starter
Stack Overflow moderator
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|