通用声明
Option Explicit
Dim imagepixels(2, 1024, 1024) As Integer '用来存储读入的图像数据
Dim picturename, picture_savename As String
I、打开文件
Private Sub open_Click()
Dim i As Integer, j As Integer
Dim red As Long, green As Long, blue As Long
Dim pixel As Long
' 设置"CancelError"为 True
CommonDialog1.CancelError = True
On Error GoTo ErrHandler ' 设置标志
CommonDialog1.Flags = cdlOFNHideReadOnly ' 设置过滤器
CommonDialog1.filter = "All Files (*.*)|*.*|Text Files" & _
"(*.txt)|*.txt|pictures(*.gif)|*.gif|pictures(*.bmp)|*.bmp" ' 指定缺省的过滤器
CommonDialog1.FilterIndex = 4 ' 显示"打开"对话框
CommonDialog1.ShowOpen ' 显示选定文件的名字
picturename = CommonDialog1.FileName
If picturename = "" Then Exit Sub
Picture1.Picture = LoadPicture(picturename)
Picture2.Picture = Picture1.Picture
Picture1.Refresh
Picture2.Refresh
Picture1.AutoSize = True
x = Picture1.ScaleWidth
y = Picture1.ScaleHeight
form1.Visible = False
For i = 0 To y - 1
For j = 0 To x - 1
pixel& = form1.Picture1.Point(j, i)
red = pixel& Mod 256
green = ((pixel& And &HFF00) / 256&) Mod 256&
blue = (pixel& And &HFF0000) / 65536
imagepixels(0, j, i) = red '分别存储像素点的GRB值
imagepixels(1, j, i) = green
imagepixels(2, j, i) = blue
Next
Next
form1.Visible = True
form1.Show
ErrHandler:
' 用户按了"取消"按钮
Exit Sub
End Sub
II、保存文件
Private Sub save_Click()
CommonDialog2.CancelError = True ' 初始化"CancelError"为 True
On Error GoTo ErrHandler ' 设置标志
CommonDialog2.Flags = cdlOFNHideReadOnly ' 设置过滤器
CommonDialog2.filter = "All Files (*.*)|*.*|Text Files" & _
"(*.txt)|*.txt|pictures(*.gif)|*.gif|pictures(*.bmp)|*.bmp" ' 指定缺省的过滤器
CommonDialog2.FilterIndex = 4 ' 显示"打开"对话框
CommonDialog2.ShowSave ' 显示选定文件的名字
picture_savename = CommonDialog2.FileName
SavePicture Picture1.Image, picture_savename
ErrHandler: ' 用户按了"取消"按钮
Exit Sub
End Sub
III.退出系统
Private Sub exit_Click()
Unload Me
End Sub
IV. 24位真彩色图像加柔
Private Sub msmoothit_Click() '对24位真彩色图像进行加柔
Dim i As Integer, j As Integer
Dim dx As Integer, dy As Integer
Dim red As Long, green As Long, blue As Long
Dim gray
Dim YofImg, UofImg, VofImg, redr, greeng, blueb '记录需削波的象素点的yuv值。
If Picture1.Picture = 0 Then
MsgBox ("please choose an image,firstly")
Exit Sub
End If
ProgressBar1.Visible = True
For i = 1 To y - 1
For j = 1 To x - 1
red = (imagepixels(0, j - 1, i - 1) imagepixels(0, j - 1, i) imagepixels(0, j - 1, i 1) imagepixels(0, j, i - 1) imagepixels(0, j, i) imagepixels(0, j, i 1) imagepixels(0, j 1, i - 1) imagepixels(0, j 1, i) imagepixels(0, j 1, i 1)) / 9
green = (imagepixels(1, j - 1, i - 1) imagepixels(1, j - 1, i) imagepixels(1, j - 1, i 1) imagepixels(1, j, i - 1) imagepixels(1, j, i) imagepixels(1, j, i 1) imagepixels(1, j 1, i - 1) imagepixels(1, j 1, i) imagepixels(1, j 1, i 1)) / 9
blue = (imagepixels(2, j - 1, i - 1) imagepixels(2, j - 1, i) imagepixels(2, j - 1, i 1) imagepixels(2, j, i - 1) imagepixels(2, j, i) imagepixels(2, j, i 1) imagepixels(2, j 1, i - 1) imagepixels(2, j 1, i) imagepixels(2, j 1, i 1)) / 9
Picture1.PSet (j, i), RGB(red, green, blue)
Next
Picture1.Refresh
ProgressBar1.Value = i * 100& / (y - 1)
DoEvents
Next
MsgBox ("图像已经被加柔!")
frmmain.ProgressBar1.Visible = False
End Sub
运行过程图:

图2:程序运行中

图3:处理完
处理前后图的比较:

图4:原始图

图5:处理后的效果图
结束语
对图像采用不同的柔化算子,可以得到不同程度的柔化效果,您可以自已动手,参照上面的程序,对喜欢的照片定制柔化。
