PDA

Click to See Complete Forum and Search --> : Blurring


schnarf283
Dec 20th, 2000, 06:17 PM
I need a sub that will blur an image in a picture box by an amount that can be passed in the parameters. Thanks!

amitabh
Dec 21st, 2000, 07:55 AM
I have never tried myself, but I have seen in the forum that AnimateWindow can produce some kind of blurring. If you do succeed, then post the message.

Thanks

oetje
Dec 21st, 2000, 11:53 AM
I don't know why people ask big questions like this... The only answer is, learn to program.:p
Here's an example of it.http://www.planet-source-code.com/xq/ASP/txtCodeId.10284/lngWId.1/qx/vb/scripts/ShowCode.htm

Jotaf98
Dec 26th, 2000, 10:20 AM
Paste this into a module:



'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.