|
-
Nov 20th, 2004, 05:17 AM
#1
Thread Starter
Hyperactive Member
blur an image
I'm trying to blur an image in my picturebox.
I can't find anything on this forum what I need and nothing on google.
I was thinking,
If I have a black&White Image,
all I have to do is look for a black pixel and copy the pixel with more brightness on several target locations around the found pixel, right?
On colored pictures the same.
But If the copied pixel falls on another pixel, I think I have to weigh the colors and choose the one between them, to be the target color.
I've tried something out, but my coding is to slow anyway.
If anyone has a blur code, please tell me
and otherwise give me some hints to do it.
For example, I don't know how to brighten a color.
-
Nov 20th, 2004, 08:09 AM
#2
Thread Starter
Hyperactive Member
OK, what's HDC of a picturebox?
I've found this on the forum:
Paste this into a module:
code:--------------------------------------------------------------------------------
'To draw/extract pixels...
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Type RGBColor
Red As Integer
Green As Integer
Blue As Integer
End Type
Public Sub SmoothImage(TargetHDC As Long, Width As Long, Height As Long)
' -- These are the variables --
'Loop counters and temporary variable for colors
Dim cX As Long, cY As Long, TempColor As Long
'Temporary variables for RGB values
Dim R As Long, G As Long, B As Long
'A version of the image with all the RGBs extracted
Dim ImageRGB() As RGBColor
' -- Initializations... --
'Resize the array
ReDim ImageRGB(-1 To Width, -1 To Height)
' -- Get all the RGB values to an array --
For cX = -1 To Width
For cY = -1 To Height
'Extract this pixel
TempColor = GetPixel(TargetHDC, cX, cY)
'Extract its RGBs
ImageRGB(cX, cY).Red = TempColor And 255
ImageRGB(cX, cY).Green = (TempColor And 65280) \ 256
ImageRGB(cX, cY).Blue = (TempColor And 16711680) \ 65535
Next cY
Next cX
' -- Blend and draw them --
For cX = 0 To Width - 1
For cY = 0 To Height - 1
'Get the average between all the surrounding colors
R = (ImageRGB(cX, cY).Red + _
ImageRGB(cX + 1, cY).Red + _
ImageRGB(cX - 1, cY).Red + _
ImageRGB(cX, cY + 1).Red + _
ImageRGB(cX, cY - 1).Red + _
ImageRGB(cX - 1, cY - 1).Red + _
ImageRGB(cX + 1, cY + 1).Red + _
ImageRGB(cX + 1, cY - 1).Red + _
ImageRGB(cX - 1, cY + 1).Red) \ 9
G = (ImageRGB(cX, cY).Green + _
ImageRGB(cX + 1, cY).Green + _
ImageRGB(cX - 1, cY).Green + _
ImageRGB(cX, cY + 1).Green + _
ImageRGB(cX, cY - 1).Green + _
ImageRGB(cX - 1, cY - 1).Green + _
ImageRGB(cX + 1, cY + 1).Green + _
ImageRGB(cX + 1, cY - 1).Green + _
ImageRGB(cX - 1, cY + 1).Green) \ 9
B = (ImageRGB(cX, cY).Blue + _
ImageRGB(cX + 1, cY).Blue + _
ImageRGB(cX - 1, cY).Blue + _
ImageRGB(cX, cY + 1).Blue + _
ImageRGB(cX, cY - 1).Blue + _
ImageRGB(cX - 1, cY - 1).Blue + _
ImageRGB(cX + 1, cY + 1).Blue + _
ImageRGB(cX + 1, cY - 1).Blue + _
ImageRGB(cX - 1, cY + 1).Blue) \ 9
'Now, draw this pixel with the new color
SetPixel TargetHDC, cX, cY, RGB(R, G, B)
'End If
Next cY
Next cX
End Sub
--------------------------------------------------------------------------------
This way just call the function with the PictureBox's HDC, Width and Height.
It's much smoother than StuPaint's too! If it's for a painting program, give the user the option "Blurring Factor", and repeat the process that number of times to blur it even more.
It's from:
http://www.vbforums.com/showthread.p...light=blurring
Hoe do I call this function?
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
|