| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2377 人关注过本帖
标题:VB实现彩色图像变为灰度图像 代码很短 求解释
只看楼主 加入收藏
迪拜axq
Rank: 2
等 级:论坛游民
帖 子:29
专家分:11
注 册:2012-9-21
结帖率:71.43%
收藏
已结贴  问题点数:20 回复次数:2 
VB实现彩色图像变为灰度图像 代码很短 求解释
代码如下
for循环代码部分求解释 当然其他的代码也解释也非常感谢

程序代码:
Dim intbtnselected As Integer
Dim PictureName As String
Private Sub Command1_Click()
CommonDialog1.FileName = ""
CommonDialog1.Flags = VbOFNFileMustExist
CommonDialog1.Filter = "所有文件|*.*|(*.bmp)|*.bmp|(*.jpg)|*.jpg|(*.GIF)|*.gif"
CommonDialog1.FilterIndex = 2
CommonDialog1.DialogTitle = "打开文件"
CommonDialog1.Action = 1
PictureName = CommonDialog1.FileName
If PictureName = "" Then
  Exit Sub
End If

Image1.Picture = LoadPicture(PictureName)
Picture1.Width = Image1.Width
Picture1.Height = Image1.Height
Picture1.Picture = LoadPicture(PictureName)

Picture2.Width = Image1.Width
Picture2.Height = Image1.Height

End Sub

Private Sub Command2_Click()
Dim j, i As Integer
Dim X, Y As Integer
Dim n, Num As Integer
Dim bytRed, bytGreen, bytBlue, btyAverage As Long
Dim pixel As Long
i = 0
j = 0
X = Picture1.ScaleWidth
Y = Picture1.ScaleHeight

For i = 1 To Y - 1
  For j = 1 To X - 1
  pixel& = Picture1.Point(j, i)
  bytRed = pixel& Mod 256
  bytGreen = ((pixel& And &HFF00) / 256&) Mod 256&
  bytBlue = (pixel& And &HFF0000) / 65536
  btyAverage = Fix((bytRed + bytGreen + bytBlue) / 3)
  Picture2.PSet (j, i), RGB(btyAverage, btyAverage, btyAverage)
  Next
Next
End Sub

Private Sub Command3_Click()
End
End Sub

Private Sub Command4_Click()
Picture1.Picture = LoadPicture("")
Picture1.Cls

Picture2.Cls
End Sub



[ 本帖最后由 迪拜axq 于 2012-9-29 16:29 编辑 ]
搜索更多相关主题的帖子: 其他 彩色 
2012-09-29 16:27
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:10 
  For j = 1 To X - 1
  pixel& = Picture1.Point(j, i)                            '取点颜色
  bytRed = pixel& Mod 256                                  '分解出红色变量来  内存里颜色表示是 00 BB GG RR ,R在最低位,G在中间,B在最前面,共3个字节,然后最前面再加一个字节凑整为一个 long 类型
  bytGreen = ((pixel& And &HFF00) / 256&) Mod 256&         '取中间位,然后右移1字节,再求余.感觉这里求余操作是多余的步骤.我现在无法测试.
  bytBlue = (pixel& And &HFF0000) / 65536                  '取最高位,直接右移2字节
  btyAverage = Fix((bytRed + bytGreen + bytBlue) / 3)      '求平均
  Picture2.PSet (j, i), RGB(btyAverage, btyAverage, btyAverage)   '画点
  Next
收到的鲜花
  • 迪拜2012-10-01 09:32 送鲜花  3朵   附言:我很赞同

授人于鱼,不如授人于渔
早已停用QQ了
2012-10-01 07:32
迪拜
Rank: 2
等 级:论坛游民
威 望:1
帖 子:43
专家分:17
注 册:2012-9-23
收藏
得分:10 
版主威武啊 得正解
2012-10-01 09:31
快速回复:VB实现彩色图像变为灰度图像 代码很短 求解释
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052708 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved